Question

I'm trying to make a Qt program using Qt Creator and Qwt for plotting. I've never used Qt Creator before. I've created a MainWindow and added a Qwtplot widget there (object name: qwtPlot). The widget shows up in the program when I compile and run it. But theres no mention of the qwtPlot object anywhere in the (autogenerated) code so I assume it is being added at compile time from the .ui xml file (or something).

My question is that... how do I modify/change the qwtPlot object? Or where should I place the code?

I'm having a hard time articulating my question but the question basically is "How do I do anything with the qwtPlot widget which is created (graphically) with Qtcreator?". I've checked some tutorials but in the tutorials they add the widgets manually in the code, but I'd like to use Qt Creator (because my UI will be fairly complicated). This whole Qt Creator is pretty confusing...

Was it helpful?

Solution

A typical solution is to use multiple inheritance or contain the UI component as a private member. Qt explains the process on their web site:

Using a Designer UI File in Your Application

For a main window, most of this is auto-generated for you (on my version, at least ... 2.0.1)

You should have (or create) a mainwindow.h file that contains a member of type Ui::MainWindow.

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

private:
  Ui::MainWindow *ui;
};

Your mainwindow.cpp should initialize it properly, and then you can get to the auto-generated members through the private ui member.

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui->setupUi(this);
    ui->myControl->setProperty(...);
}

MainWindow::~MainWindow() {
    delete ui;
}

If you use the multiple-inheritance method, you can access the members directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top