在我的 Qt 应用程序中,我的主窗口小部件的构造函数中有以下代码。无论我如何对齐添加到 QGridLayout 的按钮,它们始终位于左上角,彼此重叠。
谁能告诉我我做错了什么,我找不到。
btn_File= new QPushButton("&File", this);
btn_Close = new QPushButton("&Close", this);
btn_File->setAutoFillBackground(true);
btn_Close->setAutoFillBackground(true);
QGridLayout * layout = new QGridLayout(this);
layout->setContentsMargins(20,20,10,10);
layout->setSpacing(5);
layout->addWidget(btn_File,2,2, Qt::AlignRight);
layout->addWidget(btn_Close,1,1);
this->setLayout(layout);
编辑:似乎只有 btn_Close
被绘制。我只是试图将 QComboBox 添加到网格中,但它没有显示出来。
最佳答案
问题是我的主窗口是从 QMainWindow
派生的,在其中您需要在添加 GUI 元素之前添加一个 CentralWidget。我改为从 QWidget
派生我的主窗口,现在它可以工作了。
关于c++ - Qt Gridlayout 不重新对齐 GUI 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8385279/