What is the rule for choosing “central widget” in QMainWindow? and why is it important?

前端 未结 2 550
攒了一身酷
攒了一身酷 2021-01-13 16:35

I understand setCentralWidget is required in QMainWindow implementation, and at first sight, it seemed extremely self-explaining what central widget means.

相关标签:
2条回答
  • 2021-01-13 17:19

    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:

    • 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();
    }
    

    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();
    }
    

    0 讨论(0)
  • 2021-01-13 17:29

    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.

    0 讨论(0)
提交回复
热议问题