Question

I have a Qt 4 base class that creates the main application dialog:

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
};

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    QWidget *central = new QWidget;
    ...
    setCentralWidget(central);
}

I must not touch this code, but I can extend it with a derived class:

class MyWindow : public MainWindow {
    Q_OBJECT
public:
    explicit MyWindow(QWidget *parent = 0);
};

MyWindow::MyWindow(QWidget *parent)
{
    MainWindow(parent);
    QWidget *myWidget = new QWidget;
    //???
}

I would like to add more widgets to the base class' central widget. How can I do it once setCentralWidget() has already been called in the base class?

Was it helpful?

Solution

You can get access to central widget, using QMainWindow::centralWidget() and add widgets or layouts to its layout, knowing its structure. A simple example:

QMainWindow::centralWidget()->layout()->addWidget(new QLabel(tr("New label")));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top