如何在Qt中使用最小布局进行复杂设计

如何在Qt中使用最小布局进行复杂设计,qt,Qt,我有复杂的GUI设计,我希望尽量少使用QLayout以获得更可读的代码。 我使用了QGridLayout和QBoxLayout,但代码不可读。 我的代码的简短版本请建议修改,这样代码将变得更可读 QVBoxLayout *mainLayout = new QVBoxLayout(this); // Main Layout // First GroupBox QGroupBox QGroupBox *GroupBox1 = new QGroupBox(); QHBoxLayout *La

我有复杂的GUI设计,我希望尽量少使用QLayout以获得更可读的代码。 我使用了QGridLayout和QBoxLayout,但代码不可读。 我的代码的简短版本请建议修改,这样代码将变得更可读

QVBoxLayout *mainLayout   = new QVBoxLayout(this); // Main Layout 

// First GroupBox  QGroupBox
QGroupBox *GroupBox1 = new QGroupBox();
QHBoxLayout *Layout1 = new QHBoxLayout(); // Layout1 contains 3 Line edits
GroupBox1->setLayout(Layout1);

// Second Group table Group Box
QGroupBox *GroupBox2 = new QGroupBox("Second Group", this);
//   GroupBox2 contains table Widget and Buttons

QHBoxLayout *tableWrapperLayout = new QHBoxLayout();
QVBoxLayout *TableWidgetLayout1  = new QVBoxLayout();
QVBoxLayout *TableButtonLayout1  = new QVBoxLayout();
GroupBox2->setLayout(tableWrapperLayout);

// Param Table widget
QGroupBox *GroupBox3 = new QGroupBox("Third Group", this); // Same as GroupBox3

QHBoxLayout *TableWrapperLayout1  = new QHBoxLayout();
QVBoxLayout *TableWidgetLayout2 = new QVBoxLayout();
QVBoxLayout *TableButtonLayout2 = new QVBoxLayout();

GroupBox3->setLayout(TableWrapperLayout1);

QHBoxLayout *groupWrapperLayout = new QHBoxLayout();
// This is Wrapper for GroupBox2     and GroupBox3
groupWrapperLayout->addWidget(GroupBox2);
groupWrapperLayout->addWidget(GroupBox3);

// Main Layout contains GroupBox1 and groupWrapper
mainLayout->addWidget(GroupBox1);
mainLayout->addLayout(groupWrapperLayout);

我不知道它有什么不可读的地方,但这里有一点不太详细。一般来说,您不需要使用方框布局,网格布局可以完成方框布局所能完成的所有工作。您也不需要为由布局管理的小部件设置父级

在真正的代码中,您当然不会在堆上创建所有按钮/编辑,而是简单地将它们添加为
小部件
类的成员,然后将它们逐个添加到布局中。我使用循环和堆分配只是为了简洁

#include <QApplication>
#include <QGridLayout>
#include <QLineEdit>
#include <QGroupBox>
#include <QTableWidget>
#include <QPushButton>

class Widget : public QWidget {
    QGridLayout m_layout;
    QGroupBox m_group1;
    QGridLayout m_group1Layout;
    QGroupBox m_group2;
    QGridLayout m_group2Layout;
    QGroupBox m_group3;
    QGridLayout m_group3Layout;
public:
    Widget(QWidget * parent = 0) : QWidget(parent),
        m_layout(this),
        m_group1("First Group"),
        m_group1Layout(&m_group1),
        m_group2("Second Group"),
        m_group2Layout(&m_group2),
        m_group3("Third Group"),
        m_group3Layout(&m_group3)
    {
        m_layout.addWidget(&m_group1, 0, 0, 1, 2);
        m_layout.addWidget(&m_group2, 1, 0);
        m_layout.addWidget(&m_group3, 1, 1);

        // Line edits in group 1
        for (int i = 0; i < 3; ++ i)
            m_group1Layout.addWidget(new QLineEdit, 0, i);

        // Table and buttons in group 2
        m_group2Layout.addWidget(new QTableWidget, 0, 0, 4, 1);
        for (int i = 0; i < 4; ++ i)
            m_group2Layout.addWidget(new QPushButton(QString::number(i)), i, 1);

        // Table and buttons in group 3
        m_group3Layout.addWidget(new QTableWidget, 0, 0, 4, 1);
        for (int i = 0; i < 4; ++ i)
            m_group3Layout.addWidget(new QPushButton(QString::number(i)), i, 1);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

#包括
#包括
#包括
#包括
#包括
#包括
类Widget:publicqwidget{
QGridLayout Mu布局;
QGroupBox m_group1;
QGridLayout m_组1布局;
QGroupBox m_group2;
QGridLayout m_组2布局;
QGroupBox m_group3;
QGridLayout m_组3布局;
公众:
小部件(QWidget*parent=0):QWidget(parent),
m_布局(本),
m_集团1(“第一集团”),
m_组1布局(&m_组1),
m_集团2(“第二集团”),
m_组2布局(&m_组2),
m_集团3(“第三集团”),
m_组3布局(&m_组3)
{
m_layout.addWidget(&m_group1,0,0,1,2);
m_layout.addWidget(&m_group2,1,0);
m_layout.addWidget(&m_group3,1,1);
//组1中的行编辑
对于(int i=0;i<3;++i)
m_group1Layout.addWidget(新的QLineEdit,0,i);
//第2组中的表格和按钮
m_group2Layout.addWidget(新的QTableWidget,0,0,4,1);
对于(int i=0;i<4;++i)
m_group2Layout.addWidget(新的QPushButton(QString::number(i)),i,1);
//第3组中的表格和按钮
m_group3Layout.addWidget(新的QTableWidget,0,0,4,1);
对于(int i=0;i<4;++i)
m_group3Layout.addWidget(新的QPushButton(QString::number(i)),i,1);
}
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
小部件w;
w、 show();
返回a.exec();
}

使用QtCreator,不要担心ui类的可读性,只参考您需要的小部件。请发布一些代码,说明代码的“不可读性”。“到目前为止,你的问题太宽泛了。”KubaOber我用过很多布局框。我想我应该使用GridLayout而不是水平和垂直框。非常感谢!但我不明白这句话“你不需要为由布局管理的小部件设置父项”你能解释一下吗。再次感谢您。@D'JayPatil而不是
newqgroupbox(“第二组”),这)
只需执行
newqgroupbox(“第二组”)
。对于将由布局管理的小部件,
这个
父项是不必要的。好的,我知道了。感谢您的宝贵建议和解释。