选择“中央窗口小部件"的规则是什么?在QMainWindow中?为什么重要呢? [英] What is the rule for choosing "central widget" in QMainWindow? and why is it important?

查看:71
本文介绍了选择“中央窗口小部件"的规则是什么?在QMainWindow中?为什么重要呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 QMainWindow 实现中需要 setCentralWidget ,而且乍一看,这似乎是非常不言自明的中央控件的含义.但是," central "是否有更严格的定义?

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?

Qt文档对此没有任何说明,除了简单地声明中央窗口小部件很重要之外,这不是很有帮助.

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

推荐答案

setCentralWidget()方法中的 central 词与重要性无关,但是如果您检查具有QMainWindow的布局,我们将看到它处于中心位置:

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.

非中央窗口小部件会发生什么?中央窗口小部件和非中央窗口小部件之间是否存在某些差异,这些差异稍后可能会影响它们的行为?没有区别.

在上方的中央小部件框中说,我相对于确切的中心点对称地放置了两个QLabel.在这种情况下,哪个QLabel应该是中央小部件?哪一个很好?

您不必选择,您可以将2个QLabels成为centralWigdet的一部分,centralwidget仅指中心位置,例如:

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

如果任何一个小部件都可以,为什么QMainWindow如此迫切地要求设置中央小部件?

您不必一定要设置一个中央窗口小部件,但是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();
}

这篇关于选择“中央窗口小部件"的规则是什么?在QMainWindow中?为什么重要呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆