theArcticOcean

博观而约取,厚积而薄发

QT Layout中增删widget与切换界面

背景:以QGroupBox为father widget,用VBoxLayout的布局排版多个checkBox。接下来解决三个问题:
(1)在VBoxLayout中增添checkBox并显示
(2)从VBoxLayout中删除checkBox
(3)切换groupBox中的界面

在QGroupBox中以VBoxLayout添加子widget (checkBox)

失败的案例:


这里写图片描述

code:

    ui->setupUi(this);
    QVBoxLayout layout;

    QCheckBox box1("box1");
    QCheckBox box2("box2");
    QCheckBox box3("box3");

    layout.addStretch(1);
    layout.addWidget(&box1);
    layout.addStretch(1);
    layout.addWidget(&box2);
    layout.addStretch(1);
    layout.addWidget(&box3);
    layout.addStretch(1);

    wui->groupBox->setLayout(&layout);

关键点:
(1)需要在groupBox中加上一个father widget: widget
(2)设置widget的几何形状和groupBox保持一致,需注意的是widget设置geometry的时候QRect是相对于groupBox而言的。
(3)在widget中以VBoxLayout的方式添加子widget: checkBox ,且要注明checkBox的father widget是widget。
(4)最后要将widget show出来。

    ui->setupUi(this);
    QVBoxLayout layout;
    QWidget *widget = new QWidget(ui->groupBox);
    QRect rect = ui->groupBox->geometry();
    rect.setX(0);   // X and Y is related to parent widget
    rect.setY(0);

    widget->setGeometry(rect);
    widget->setWindowTitle("hello");

    QCheckBox *box1 = new QCheckBox("box1",widget);     //set box's father is widget
    QCheckBox *box2 = new QCheckBox("box2",widget);
    QCheckBox *box3 = new QCheckBox("box3",widget);

    layout.addStretch(1);
    layout.addWidget(box1);     // addStrech means allocate extra space base on factor param
    layout.addStretch(2);
    layout.addWidget(box2);
    layout.addStretch(2);
    layout.addWidget(box3);
    layout.addStretch(2);

    widget->setLayout(&layout);
    widget->show(); // show means update layout


这里写图片描述

checkBox和groupBox的父子关系决定了check能否显示出来,Layout则规定了checbBox显示布局。

从VBoxLayout中删除子widget(checkBox)

在QLayout中有这样一个方法:void QLayout::removeWidget(QWidget * widget)
不过,这只能确保子widget和Layout没有了关系,子widget和父widget还存在关系。所以单纯使用removeWidget不能删除父widget中的子widget,哪怕将Layout删除也不行。如果要删除子widget,必须将子widget从layout和父widget都删除才行。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
   // ...
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked(bool checked)
{
    if(checked){
        layout->removeWidget(box1);
        layout->removeWidget(box2);
        layout->removeWidget(box3);
        delete layout;
        layout = NULL;
        delete box1;
        delete box2;
        delete box3;
    }
    else {
        layout = new QVBoxLayout();
        box1 = new QCheckBox("box1",widget);    //set box's father is widget
        box2 = new QCheckBox("box2",widget);
        box3 = new QCheckBox("box3",widget);

        layout->addStretch(1);
        layout->addWidget(box1);    // addStrech means allocate extra space base on factor param
        layout->addStretch(2);
        layout->addWidget(box2);
        layout->addStretch(2);
        layout->addWidget(box3);
        layout->addStretch(2);
        widget->setLayout(layout);
    }
}


这里写图片描述

切换groupBox中的界面

还是以上面的例子做说明,假设点击button切换两个checkBox列表,我想保存checkBox的勾选状态并再次显示出来,这应该怎么做呢?
可以做两个父widget,将checkBox放进不同的widget中。再在button的click(bool)事件中通过hide(),show()切换这两个页面。

.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QWidget>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    QWidget *widget;
    QWidget *widget2;
    QCheckBox *box1;
    QCheckBox *box2;
    QCheckBox *box3;

    QCheckBox *box2_1;
    QCheckBox *box2_2;
    QCheckBox *box2_3;

    QVBoxLayout *layout;
    QVBoxLayout *layout2;

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void widget1_init();
    void widget2_init();
private slots:
    void on_pushButton_clicked(bool checked);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

cpp文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    layout = new QVBoxLayout();
    layout2 = new QVBoxLayout();
    widget1_init();
    widget2_init();

    ui->pushButton->setCheckable(true);
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::widget1_init()
{
    widget = new QWidget();

    QRect rect = ui->groupBox->geometry();
    rect.setX(0);  // X and Y is related to parent widget
    rect.setY(0);

    widget->setGeometry(rect);

    box1 = new QCheckBox("box1",widget);    //set box's father is widget
    box2 = new QCheckBox("box2",widget);
    box3 = new QCheckBox("box3",widget);

    layout->addStretch(1);
    layout->addWidget(box1);    // addStrech means allocate extra space base on factor param
    layout->addStretch(2);
    layout->addWidget(box2);
    layout->addStretch(2);
    layout->addWidget(box3);
    layout->addStretch(2);

    widget->setLayout(layout);
}

void MainWindow::widget2_init()
{
    widget2 = new QWidget();

    QRect rect = ui->groupBox->geometry();
    rect.setX(0);  // X and Y is related to parent widget2
    rect.setY(0);

    widget2->setGeometry(rect);

    box2_1 = new QCheckBox("box2_1",widget2);    //set box's father is widget2
    box2_2 = new QCheckBox("box2_2",widget2);
    box2_3 = new QCheckBox("box2_3",widget2);

    layout2->addStretch(1);
    layout2->addWidget(box2_1);    // addStrech means allocate extra space base on factor param
    layout2->addStretch(2);
    layout2->addWidget(box2_2);
    layout2->addStretch(2);
    layout2->addWidget(box2_3);
    layout2->addStretch(2);

    widget2->setLayout(layout2);
}

void MainWindow::on_pushButton_clicked(bool checked)
{
    if(checked){
        widget->setParent(ui->groupBox);
        widget->show();
        widget2->hide();

        ui->groupBox->setTitle("widget");
    }
    else {
        widget->hide();
        widget2->setParent(ui->groupBox);
        widget2->show();

        ui->groupBox->setTitle("widget2");
    }
}


这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/theArcticOcean/article/details/78161269
文章标签: qt widget Layout
个人分类: qt
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

QT Layout中增删widget与切换界面

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭