0

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?

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Pietro
  • 12,086
  • 26
  • 100
  • 193
  • When I copy this example, I get declaration of parent MainWindow(parent) shadows a parameter pointing at parent. – mLstudent33 May 30 '20 at 23:07

1 Answers1

3

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")));
Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Amartel
  • 4,248
  • 2
  • 15
  • 21