QT5学习:窗口布局测试

时间:2021-09-03 03:51:55


(1)、QWidget窗体显示与隐藏:

    if(detailWidget->isHidden())
        detailWidget->show();//QWidget窗体显示
    else
        detailWidget->hide();//QWidget窗体隐藏

(2)、

1. 直接添加两个按钮,则按钮大小会随前面控件:

    QGridLayout *LeftLayout =new QGridLayout;
    LeftLayout->addWidget(nameLabel,0,0);
    LeftLayout->addWidget(nameLineEdit,0,1);
    LeftLayout->addWidget(sexLabel);
    LeftLayout->addWidget(sexComboBox);

    QPushButton *OKBtn =new QPushButton(tr("确定"));
    QPushButton *DetailBtn =new QPushButton(tr("详细"));
//    //QDialogButtonBox *btnBox =new QDialogButtonBox(Qt::Vertical);//对话框按钮组,按钮竖直放
//    QDialogButtonBox *btnBox =new QDialogButtonBox(Qt::Horizontal);//对话框按钮组,按钮水平放一排
//    btnBox->addButton(OKBtn,QDialogButtonBox::ActionRole);
//    btnBox->addButton(DetailBtn,QDialogButtonBox::ActionRole);
    LeftLayout->addWidget(OKBtn);
    LeftLayout->addWidget(DetailBtn);

    //QHBoxLayout *mainLayout =new QHBoxLayout(baseWidget);
    QVBoxLayout *mainLayout =new QVBoxLayout(baseWidget);
    mainLayout->addLayout(LeftLayout);

QT5学习:窗口布局测试


2. 以对话框按钮组方式加入到布局中,则按钮会正常显示:

<span style="color:#c0c0c0;">    </span><span style="color:#800080;">QGridLayout</span><span style="color:#c0c0c0;"> </span><span style="color:#000000;">*</span><span style="color:#000000;">LeftLayout</span><span style="color:#c0c0c0;"> </span><span style="color:#000000;">=</span><span style="color:#808000;">new</span><span style="color:#c0c0c0;"> </span><span style="color:#800080;">QGridLayout</span><span style="color:#000000;">;</span><pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style="color:#c0c0c0;">    </span><span style="color:#000000;">LeftLayout</span><span style="color:#000000;">-></span><span style="color:#000000;">addWidget</span><span style="color:#000000;">(</span><span style="color:#000000;">nameLabel</span><span style="color:#000000;">,</span><span style="color:#000080;">0</span><span style="color:#000000;">,</span><span style="color:#000080;">0</span><span style="color:#000000;">);</span>
    LeftLayout->addWidget(nameLineEdit,0,1);
    LeftLayout->addWidget(sexLabel);
    LeftLayout->addWidget(sexComboBox);
 
    QPushButton *OKBtn =new QPushButton(tr("确定"));
    QPushButton *DetailBtn =new QPushButton(tr("详细"));
    //QDialogButtonBox *btnBox =new QDialogButtonBox(Qt::Vertical);//对话框按钮组,按钮竖直放
    QDialogButtonBox *btnBox =new QDialogButtonBox(Qt::Horizontal);//对话框按钮组,按钮水平放一排
    btnBox->addButton(OKBtn,QDialogButtonBox::ActionRole);
    btnBox->addButton(DetailBtn,QDialogButtonBox::ActionRole);
    //LeftLayout->addWidget(OKBtn);
    //LeftLayout->addWidget(DetailBtn);
 
    //QHBoxLayout *mainLayout =new QHBoxLayout(baseWidget);
    QVBoxLayout *mainLayout =new QVBoxLayout(baseWidget);
    mainLayout->addLayout(LeftLayout);
    mainLayout->addWidget(btnBox);//以对话框按钮组方式加入到布局中
 

QT5学习:窗口布局测试