简体   繁体   English

如何在Qt Designer中使小部件自动拉伸到父小部件的大小

[英]How to make widgets automatically stretch to the parent widget's size in Qt Designer

I need to add a TabWidget control to the MainWindow and place TableWidget in every tab it has. 我需要将TabWidget控件添加到MainWindow并将TableWidget放置在它具有的每个选项卡中。 The problem is that I need to make them automatically stretch to the parent widget's size ( TabWidget to the window's size, TableWidget 's to the TabWidget 's size). 问题是我需要使它们自动拉伸到父窗口小部件的大小( TabWidget到窗口的大小, TableWidgetTabWidget的大小)。

What is the easiest way to achieve it via Qt Designer? 通过Qt Designer实现它的最简单方法是什么?

You should use Qt layouts. 您应该使用Qt布局。

In designer you have to select the widget you want to have its children laid out properly, then choose Form -> Lay Out Vertically/Horizontally/... shortcuts: Ctrl+1...6 在设计器中,您必须选择要正确放置其子项的小部件,然后选择“ 表单” -> “垂直/水平/ ...布局”快捷键: Ctrl+1...6

Below is an example program that will create a view as you needed. 下面是一个示例程序,它将根据需要创建一个视图。 Logic is Mainwindow-> Central Widget-> Add Vertical Layout-> Add Tab Widget 逻辑是主窗口->中央小部件->添加垂直布局->添加选项卡小部件
->Add Tab 1->Add V Box Layout -> Add Table 1 (5 X 5) ->添加选项卡1->添加垂直框布局->添加表1(5 X 5)
->Add Tab 2->Add V Box Layout -> Add Table 1 (5 X 5) ->添加选项卡2->添加V框布局->添加表1(5 X 5)

Code comment will explain in detail. 代码注释将详细解释。

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);
}

在此处输入图片说明

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

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