我正在使用 Creator 来构建主主窗口,并用我的所有小部件填充它。 我在此阶段没有设置任何主窗口布局(例如“在网格中布局”或“水平布局”。
当我启动应用程序时,我想通过按左键将其中小部件的主窗口布局动态更改为“在网格中布局”,就像在创建者模式中一样。
我已经努力尝试了所有可能的组合,阅读了很多帖子。 这个解决方案: Qt:无法在 QMainWindow 中设置布局 不起作用,对我来说没有多大意义。
我已经尝试过:
QGridLayout * MainWindowLayout = new QGridLayout;
ui->setupUi(this);
centralWidget()->setLayout(MainWindowLayout);
没有运气
我尝试在设计时将所有小部件放入一个名为 MainWindowWidget 的大小部件中,然后将其设置为centralWidget
QGridLayout * MainWindowLayout = new QGridLayout;
ui->setupUi(this);
setCentralWidget(ui->MainWindowWidget);
centralWidget()->setLayout(MainWindowLayout);
没有运气
在使用 Creator 时,有没有办法在设计时更改 MainWindow 小部件的布局,例如“在网格中放置”??
编辑: 更具体地说,没有运气,我的意思是小部件没有按预期放置在网格中。 这是一个代码片段,您可以在空应用程序上尝试
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
/*
Place some widgets at design time with Creator (at least 2) in the MainWindow form
in a misplaced order and do not apply any "Lay out xxx" right button on QT Creator
*/
ui->setupUi(this);
/* HERE I WANT THE MainWindow or either an Object to take a specific Layout */
QGridLayout * MainWindowLayout = new QGridLayout;
ui->setupUi(this);
centralWidget()->setLayout(MainWindowLayout);
}
我谷歌搜索了快两天了,找不到任何出路
谢谢大家的帮助...
您正在创建
layout
但您没有向其中添加 widgets
。这应该可以解决您的问题:
ui->setupUi(this);
QGridLayout *MainWindowLayout = new QGridLayout();
MainWindowLayout->addWidget(ui->label, 0, 0);
MainWindowLayout->addWidget(ui->label_2, 0, 1);
// Add all other widgets to your layout...
centralWidget()->setLayout(MainWindowLayout);
最后我做了以下工作:
我将所有表单小部件放入 3 个不同的小部件中(在我的例子中,QFrame 作为容器)。 然后我按照建议将它们放入布局中并且成功了。
QGridLayout *MainWindowLayout = new QGridLayout();
MainWindowLayout->addWidget(ui->MainFrame, 0, 0); // MainFrame --> My new object containing other widgets
MainWindowLayout->addWidget(ui->DebugButtonsFrame, 0, 1); // DebugButtonsFrame as above
MainWindowLayout->addWidget(ui->DebugMemoFrame, 1, 0); // DebugMemoFrame as above
// Add all other widgets to your layout...
centralWidget()->setLayout(MainWindowLayout);