【问题标题】:How to make widgets automatically stretch to the parent widget's size in Qt Designer如何使小部件在 Qt Designer 中自动拉伸到父小部件的大小
【发布时间】:2016-05-13 17:54:35
【问题描述】:
我需要将TabWidget 控件添加到MainWindow 并将TableWidget 放置在它的每个选项卡中。问题是我需要让它们自动拉伸到父窗口小部件的大小(TabWidget 到窗口的大小,TableWidget 到 TabWidget 的大小)。
通过 Qt Designer 实现它的最简单方法是什么?
【问题讨论】:
标签:
c++
qt
layout
qt5
qt-creator
【解决方案1】:
你应该使用 Qt 布局。
在设计器中,您必须选择要使其子级正确布局的小部件,然后选择 Form -> Lay Out Vertically/Horizontally/... 快捷方式:Ctrl+1...6
【解决方案2】:
下面是一个示例程序,它将根据您的需要创建一个视图。逻辑是 Mainwindow-> Central Widget-> Add Vertical Layout-> Add Tab Widget
->添加选项卡 1->添加 V 框布局 -> 添加表 1 (5 X 5)
->添加选项卡2->添加V框布局->添加表1(5 X 5)
代码注释会详细解释。
void MainWindow::fnInit()
{
//Layout to the MainWindow
QVBoxLayout *vLayoutMain;
QTabWidget *Tab;
QWidget *Widget1;
//Layout to the Tab1
QVBoxLayout *vLayoutTab1;
QTableWidget *Table1;
QWidget *Widget2;
//Layout to the Tab2
QVBoxLayout *vLayoutTab2;
QTableWidget *Table2;
//Set Vertical Box Layout to the main Window (central widget)
vLayoutMain = new QVBoxLayout(this->centralWidget());
Tab = new QTabWidget(this->centralWidget());
Widget1 = new QWidget();
//Set the Vertical Box Layout to the Widget 1 (holds the tab1)
vLayoutTab1 = new QVBoxLayout(Widget1);
Table1 = new QTableWidget(5,5,Widget1);
vLayoutTab1->addWidget(Table1);
Tab->addTab(Widget1, QString("Tab 1"));
Widget2 = new QWidget();
//Set the Vertical Box Layout to the Widget 2 (holds the tab2)
vLayoutTab2 = new QVBoxLayout(Widget2);
Table2 = new QTableWidget(5,5,Widget2);
vLayoutTab2->addWidget(Table2);
Tab->addTab(Widget2, QString("Tab 2"));
//Adding the Tab widget to the main layout
vLayoutMain->addWidget(Tab);
Tab->setCurrentIndex(0);
}