5
votes

I understand setCentralWidget is required in QMainWindow implementation, and at first sight, it seemed extremely self-explaining what central widget means. But is there a more strict definition of "central"?

Say, I have several equally important widgets located in the central area of the window, should I always find a way to group them together and set the group to be the central widget? or I could just randomly choose one?

More importantly, what happens to the non-central widgets? Are there certain differences between central and non-central widgets that might impact their behaviors later?

The Qt documentation says nothing about this, other than simply stating central widget is important, which is not very helpful.

2

2 Answers

7
votes

The central word in the setCentralWidget() method has nothing to do with the importance but if you check the layout that has the QMainWindow we will see that it is in the central position:

enter image description here

  • should I always find a way to group them together and set the group to be the central widget? Or I could just randomly choose one? There can only be one centralwidget, so if you want to have several widget in the central position you must create a new widget that is the container and set the other widget through the layouts.

  • what happens to the non-central widgets? Are there certain differences between central and non-central widgets that might impact their behaviors later? There's no difference.


Say in your central widget box above, I place two QLabels symmetrically with respect to the exact central point. In this case, which QLabel should be the central widget? Either one is fine?

You do not have to choose, you can the 2 QLabels be part of the centralWigdet, centralwidget only refers to the central position, for example:

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow w;
    QLabel left_label("left");
    left_label.setAlignment(Qt::AlignCenter);
    QLabel right_label("rigth");
    right_label.setAlignment(Qt::AlignCenter);
    QWidget *central_widget = new QWidget;
    QHBoxLayout *lay = new QHBoxLayout(central_widget);
    lay->addWidget(&left_label);
    lay->addWidget(&right_label);
    w.setCentralWidget(central_widget);
    w.show();
    return a.exec();
}

enter image description here

If either widget is fine, why is setting central widget so critically required by QMainWindow?

You do not necessarily have to set a centralwidget, but QMainWindow unlike other widget already has a certain layout, so if you want to place the widgets you must use that method.

The centralwidget refers to a relative position but it is not exactly a central position:

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow w;
    QLabel *central_widget = new QLabel("Central Widget");
    central_widget->setAlignment(Qt::AlignCenter);
    w.setCentralWidget(central_widget);
    QDockWidget *dock = new QDockWidget("left");
    w.addDockWidget(Qt::LeftDockWidgetArea, dock);
    dock->setWidget(new QTextEdit);
    w.show();
    return a.exec();
}

enter image description here

1
votes

Usually you don't need a QMainWindow!

If you don't need docking nor MDI, then don't use QMainWindow: a QWidget or a QDialog will do instead.

Central means "in the center", as in "in the middle", not as in "important!". QMainWindow's provides docking and multiple document interface (MDI) functionality, and there the notion of a central widget is useful. There can only be one central widget, so "which of many" should be made central is moot: you can't have more than one. One of the fundamental aspects of Qt object design is that QObject is a container of other objects, and so is a QWidget: it's a canvas that you can place other widgets on. So, if you absolutely need docking and/or MDI, then the central widget will be any QWidget that you put other non-docking widgets on. That' all.