简体   繁体   English

配置QWidget以通过布局填充父级

[英]Configure QWidget to fill parent via layouts

I'm working on a QMainWindow application and came across the following problem: I have a QMainWindow which has a QWidget as centralWidget and this widget in turn has another QWidget as child that should completely fill the first one (see code below). 我正在研究QMainWindow应用程序,并遇到了以下问题:我有一个QMainWindow ,它有一个QWidget作为centralWidget而这个小部件又有另一个QWidget作为子项,应该完全填充第一个(见下面的代码)。

To achieve this I used layouts. 为了达到这个目的,我使用了布局。 But after placing the second widget into a layout and applying this layout to the first one, the second widget still won't change its size although the first one does (when resizing the main window). 但是在将第二个窗口小部件放入布局并将此布局应用于第一个窗口小部件后,第二个窗口小部件仍然不会更改其大小,尽管第一个窗口小部件确实(当调整主窗口大小时)。

I set the background color of the first widget to green and the one of the second to red, so I would expect the resulting window to be completely red, however I get the following output: 我将第一个小部件的背景颜色设置为绿色,将第二个小部件的背景颜色设置为红色,因此我希望生成的窗口完全为红色,但是我得到以下输出:

产量

What do I have to do in order to make the second widget fill the first one and resize accordingly? 为了使第二个小部件填充第一个小部件并相应调整大小,我该怎么做?

MainWindow: 主窗口:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QGridLayout>
#include <QMainWindow>
#include <QWidget>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0) : QMainWindow(parent) {

        QWidget *p = new QWidget(this);  // first widget
        p->setStyleSheet("QWidget { background: green; }");
        this->setCentralWidget(p);

        QWidget *c = new QWidget(p);  // second widget
        c->setStyleSheet("QWidget { background: red; }");

        QGridLayout l;
        l.addWidget(c, 0, 0, 1, 1);
        p->setLayout(&l);
    }
};

#endif // MAINWINDOW_H

In your code the QGridLayout l is a local variable. 在您的代码中, QGridLayout l是一个局部变量。 This will die once the constructor code block goes out of scope. 一旦构造函数代码块超出范围,这将会死亡。 So (1) add this QGridLayout l in the class level and remaining of your code unchanged OR (2) declare it as a pointer inside the constructor as below. 所以(1)在类级别添加此QGridLayout l并保持代码的剩余不变OR(2)将其声明为构造函数内的指针,如下所示。 Code comment will explain in detail. 代码评论将详细解释。

QWidget *p = new QWidget(this);  // first widget
p->setStyleSheet("QWidget { background: green; }");
this->setCentralWidget(p);

QWidget *c = new QWidget(p);  // second widget
c->setStyleSheet("QWidget { background: red; }");

//Central widget is the parent for the grid layout.
//So this pointer is in the QObject tree and the memory deallocation will be taken care
QGridLayout *l = new QGridLayout(p); 
//If it is needed, then the below code will hide the green color in the border. 
//l->setMargin(0);
l->addWidget(c, 0, 0, 1, 1);
//p->setLayout(&l); //removed. As the parent was set to the grid layout

在此输入图像描述

//If it is needed, then the below code will hide the green color in the border. //如果需要,则下面的代码将隐藏边框中的绿色。
//l->setMargin(0); // 1-> setMargin(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM