Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 969
JackOuttaBox Avatar asked Feb 18 '19 14:02

JackOuttaBox


People also ask

What is central widget in Qt?

A central widget will typically be a standard Qt widget such as a QTextEdit or a QGraphicsView. Custom widgets can also be used for advanced applications. You set the central widget with setCentralWidget() . Main windows have either a single (SDI) or multiple (MDI) document interface.

What is the difference between QMainWindow and QWidget?

A QDialog is based on QWidget , but designed to be shown as a window. It will always appear in a window, and has functions to make it work well with common buttons on dialogs (accept, reject, etc.). QMainWindow is designed around common needs for a main window to have.


1 Answers

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

like image 98
eyllanesc Avatar answered Sep 18 '22 04:09

eyllanesc