• Qt学习26 布局管理综合实例


    Qt学习26 布局管理综合实例

    需求分析

    • 练习开发一个向导用户界面
      • 在同一个界面上展现 不同的向导页面
      • 通过 “上一步”“下一步” 按钮进行切换
      • 不同页面上的元素组件和组件排布都不相同
      • 页面中的组件通过布局管理器进行排布

    解决方案

    • 通过布局嵌套进行界面设计

    在这里插入图片描述

    • 通过 QStackedLayout 管理不同的页面

    在这里插入图片描述

    • 通过子组件的方式生成不同的页面

    在这里插入图片描述

    编程实验 - 向导用户界面

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QStackedLayout>
    #include <QPushButton>
    #include <QLabel>
    #include <QLineEdit>
    
    class Widget : public QWidget
    {
        Q_OBJECT
    private:
        QStackedLayout sLayout;
        QPushButton preBtn;
        QPushButton nextBtn;
        QLabel fLabel1;
        QLabel fLabel2;
        QLabel fLabel3;
        QLabel fLabel4;
        QLineEdit sEdit;
        QPushButton tBtn1;
        QPushButton tBtn2;
        void initControl();
        QWidget* get1stPage();
        QWidget* get2ndPage();
        QWidget* get3rdPage();
    private slots:
        void onPreBtnClicked();
        void onNextBtnClicked();
    public:
        Widget(QWidget *parent = 0);
        ~Widget();
    };
    
    #endif // WIDGET_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    #include "Widget.h"
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QGridLayout>
    #include <QFormLayout>
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        initControl();
    }
    
    Widget::~Widget()
    {
    
    }
    
    void Widget::initControl()
    {
        QVBoxLayout* vLayout = new QVBoxLayout();
        QHBoxLayout* hLayout = new QHBoxLayout();
        vLayout->addLayout(&sLayout);
        vLayout->addLayout(hLayout);
    
        preBtn.setText("pre page");
        preBtn.setMinimumSize(160, 30);
        nextBtn.setText("next page");
        nextBtn.setMinimumSize(160, 30);
    
        connect(&preBtn, SIGNAL(clicked()), this, SLOT(onPreBtnClicked()));
        connect(&nextBtn, SIGNAL(clicked()), this, SLOT(onNextBtnClicked()));
    
        hLayout->addWidget(&preBtn);
        hLayout->addWidget(&nextBtn);
    
        sLayout.addWidget(get1stPage());
        sLayout.addWidget(get2ndPage());
        sLayout.addWidget(get3rdPage());
    
        setLayout(vLayout);
    }
    
    QWidget *Widget::get1stPage()
    {
        QWidget* ret = new QWidget();
    
        QGridLayout* gLayout = new QGridLayout();
        fLabel1.setText("This");
        fLabel2.setText("is");
        fLabel3.setText("1st");
        fLabel4.setText("page");
        gLayout->addWidget(&fLabel1, 0, 0);
        gLayout->addWidget(&fLabel2, 0, 1);
        gLayout->addWidget(&fLabel3, 1, 0);
        gLayout->addWidget(&fLabel4, 1, 1);
        ret->setLayout(gLayout);
    
        return ret;
    }
    
    QWidget *Widget::get2ndPage()
    {
        QWidget* ret = new QWidget();
    
        QFormLayout* fLayout = new QFormLayout();
        sEdit.setText("This is 2nd page");
        fLayout->addRow("Hint:", &sEdit);
        ret->setLayout(fLayout);
    
        return ret;
    }
    
    QWidget *Widget::get3rdPage()
    {
        QWidget* ret = new QWidget();
        QVBoxLayout* vLayout = new QVBoxLayout();
        tBtn1.setText("This is");
        tBtn2.setText("3rd page");
        vLayout->addWidget(&tBtn1);
        vLayout->addWidget(&tBtn2);
        ret->setLayout(vLayout);
    
        return ret;
    }
    
    void Widget::onPreBtnClicked()
    {
        int index = ((sLayout.currentIndex() - 1) + 3) % 3;
        sLayout.setCurrentIndex(index);
    }
    
    void Widget::onNextBtnClicked()
    {
        int index = (sLayout.currentIndex() + 1) % 3;
        sLayout.setCurrentIndex(index);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    #include "Widget.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
    
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    注意事项

    • 任意容器类的组件 都可以指定布局管理器
    • 同一布局管理器中的组件拥有相同的父组件
    • 设置布局管理的同时 隐式的指定父子关系

    在这里插入图片描述

    • 图中 组件1组件2 被同一个布局管理器管理,拥有相同的父组件

    小结

    • 布局管理器可以 相互嵌套构成复杂用户界面
    • 任意容器组件均可设置布局管理器
    • 同一个布局管理器中的组件拥有相同父组件
    • 组件间的父子关系是 Qt 中内存管理的重要方式
  • 相关阅读:
    Android 应用模块的构建
    对抗生成网络GAN系列——CycleGAN简介及图片春冬变换案例
    MFC_系统对话框(CFileDialog、字符编码转化ANSI-UTF8-Unicode、Window记事本demo练习)
    同花顺_代码解析_技术指标_B
    RE2文本匹配实战
    学弟学妹们你连JVM虚拟机都搞不明白就不要想着去大厂实习了
    计算机竞赛 基于深度学习的人脸专注度检测计算系统 - opencv python cnn
    Elasticsearch:在 ES|QL 中使用 DISSECT 和 GROK 进行数据处理
    【浅学Java】详解网络层IP协议
    exit()函数、_exit()函数 和 _Exit()函数
  • 原文地址:https://blog.csdn.net/weixin_40743639/article/details/125627817