C++ 从代码访问在 UI 中创建的 Qt 布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12659629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:29:35  来源:igfitidea点击:

Accessing Qt Layout created in UI from code?

c++qtlayoutwidgetqt-creator

提问by tomvodi

This is probably the dumbest problem I have ever had, but I am extremely confused. I am trying to get started with layouts, but for some reason cannot figure this one out.

这可能是我遇到过的最愚蠢的问题,但我非常困惑。我正在尝试开始使用布局,但由于某种原因无法弄清楚这一点。

I have tried adding a QGridLayout via the .ui file by just drag dropping it into my project. As I want to populate the grid with widgets upon loading, I have tried to use the "gridLayout" object in the "mainwindow.h" file both before/after the this->setupui() is called.

我尝试通过 .ui 文件添加 QGridLayout,只需将其拖放到我的项目中即可。由于我想在加载时用小部件填充网格,因此我尝试在调用 this->setupui() 之前/之后使用“mainwindow.h”文件中的“gridLayout”对象。

As I couldn't figure that out, I opted to just try creating it from scratch using code, and added the following to the main.cpp file instead. This did not display either, so I am wondering how on earth I can populate the grid when the form loads.

由于我无法弄清楚,我选择尝试使用代码从头开始创建它,并将以下内容添加到 main.cpp 文件中。这也没有显示,所以我想知道如何在表单加载时填充网格。

#include <QtGui/QApplication>
#include <QtGui>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow w;

    QGridLayout *grid = new QGridLayout;

    QLabel *label1 = new QLabel("test");
    QLabel *label2 = new QLabel("test 2");

    grid->addWidget(label1, 0, 0);
    grid->addWidget(label2, 0, 1);
    w.setLayout(grid);

    w.show();
    return app.exec();
}

回答by tomvodi

Assuming, you have simply set a QGridLayout in QtDesigner to your centralWidget in the MainWindow like this:

假设,您只需将 QtDesigner 中的 QGridLayout 设置为 MainWindow 中的 centralWidget,如下所示:

enter image description here

在此处输入图片说明

you can access it in your MainWindow code in that way with the correct object name (here it is gridLayout):

您可以在 MainWindow 代码中使用正确的对象名称访问它(这里是 gridLayout):

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->gridLayout->addWidget(new QLabel("hello world"),0,0);
}

If you have set a layout in QtDesigner or in code and you want to change the layout, QWidget won't let you install another one and you will get an error message like this:

如果您在 QtDesigner 或代码中设置了布局,并且想要更改布局,QWidget 将不会让您安装另一个布局,您将收到如下错误消息:

QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout

QWidget::setLayout: 试图在已经有布局的 MainWindow "MainWindow" 上设置 QLayout ""

In this case you have to delete the existing layout at first and then install the new one like in your code above.

在这种情况下,您必须首先删除现有布局,然后像上面的代码一样安装新布局。

If you want to access the layout in your main function you can achieve this by the QObject::findChild function like this:

如果要访问主函数中的布局,可以通过 QObject::findChild 函数实现,如下所示:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    QGridLayout *gridLayout = w.findChild<QGridLayout*>("gridLayout");
    Q_ASSERT(gridLayout);
    gridLayout->addWidget(new QLabel("hello, the second"));

    w.show();
    return a.exec();
}