zoukankan      html  css  js  c++  java
  • Qt中的布局管理器

    1. 布局管理器提供相关的类对界面组件进行布局管理,能够自动排列窗口中的界面组件,窗口变化后能自动更新界面组件的大小。

    2. QLayout是Qt布局管理器的抽象基类,通过继承QLayout实现了功能各异且互补的布局管理器。

    ①QBoxLayout: QVBoxLayout, QHBoxLayout

    ②QGridLayout:

    ③QFormLayout:

    ④QStackedLayout:

    3. 综合实例(一个简单的安装向导模型)

    Widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QtGui/QWidget>
    #include <QPushButton>
    #include <QLabel>
    #include <QLineEdit>
    
    class Widget : public QWidget
    {
        Q_OBJECT
    private:
        QPushButton PreBtn;
        QPushButton NextBtn;
        QLabel Lab0;
        QLabel Lab1;
        QLabel Lab2;
        QLabel Lab3;
    
        QPushButton PushButton1;
        QPushButton PushButton2;
    
        QLineEdit LineEdit;
        void InitControl();
    
        QWidget* GetFirstPage();
        QWidget* GetSecondPage();
        QWidget* GetThirdPage();
    
    private slots:
        void PreBtnClicked();
        void NextBtnClicked();
    
    public:
        Widget(QWidget *parent = 0);
        ~Widget();
    };
    
    #endif // WIDGET_H

    Widget.cpp

    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QGridLayout>
    #include <QFormLayout>
    #include <QStackedLayout>
    #include <QDebug>
    #include "widget.h"
    
    Widget::Widget(QWidget *parent): QWidget(parent), PreBtn(this), NextBtn(this)
    {
        InitControl();
    }
    
    void Widget::InitControl()
    {
        QVBoxLayout* vLayout = new QVBoxLayout();
        QHBoxLayout* hLayout = new QHBoxLayout();
        QStackedLayout* sLayout = new QStackedLayout();
    
        PreBtn.setText("Pre Button");
        PreBtn.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        PreBtn.setMinimumSize(160, 30);
    
        NextBtn.setText("Next Button");
        NextBtn.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        NextBtn.setMinimumSize(160, 30);
    
        sLayout->addWidget(GetFirstPage());
        sLayout->addWidget(GetSecondPage());
        sLayout->addWidget(GetThirdPage());
    
        hLayout->setSpacing(10);
        hLayout->addWidget(&PreBtn);
        hLayout->addWidget(&NextBtn);
    
        vLayout->addLayout(sLayout);
        vLayout->addLayout(hLayout);
    
        setLayout(vLayout);
    
        connect(&PreBtn, SIGNAL(clicked()), this, SLOT(PreBtnClicked()));
        connect(&NextBtn, SIGNAL(clicked()), this, SLOT(NextBtnClicked()));
    
    }
    
    QWidget* Widget::GetFirstPage()
    {
        QWidget* widget = new QWidget();
        QGridLayout* gLayout = new QGridLayout();
    
        Lab0.setText("This");
        Lab1.setText("is");
        Lab2.setText("First");
        Lab3.setText("Page");
    
        gLayout->addWidget(&Lab0, 0, 0);
        gLayout->addWidget(&Lab1, 0, 1);
        gLayout->addWidget(&Lab2, 1, 0);
        gLayout->addWidget(&Lab3, 1, 1);
    
        widget->setLayout(gLayout);
    
        return widget;
    }
    
    QWidget* Widget::GetSecondPage()
    {
        QWidget* widget = new QWidget();
    
        QFormLayout* fLayout = new QFormLayout();
    
        LineEdit.setText("This is Second Page");
    
        fLayout->addRow("Name", &LineEdit);
    
        widget->setLayout(fLayout);
    
        return widget;
    }
    
    QWidget* Widget::GetThirdPage()
    {
        QWidget* widget = new QWidget();
        QVBoxLayout* vLayout = new QVBoxLayout();
    
        PushButton1.setText("Third");
    
        PushButton2.setText("Page");
    
        vLayout->addWidget(&PushButton1);
        vLayout->addWidget(&PushButton2);
    
        widget->setLayout(vLayout);
    
        return widget;
    }
    
    void Widget::PreBtnClicked()
    {
    
        QStackedLayout* sLayout =  dynamic_cast<QStackedLayout*>(((dynamic_cast<QVBoxLayout*>(layout()))->children())[0]);
    
        if(sLayout != NULL)
        {
            int currentIndex = sLayout->currentIndex();
    
            currentIndex = (currentIndex > 0) ? (currentIndex - 1) : currentIndex;
    
            sLayout->setCurrentIndex(currentIndex);
        }
    
        qDebug() << "PreBtnClicked()";
    }
    
    void Widget::NextBtnClicked()
    {
        QStackedLayout* sLayout =  dynamic_cast<QStackedLayout*>(((dynamic_cast<QVBoxLayout*>(layout()))->children())[0]);
    
        if(sLayout != NULL)
        {
            int currentIndex = sLayout->currentIndex();
    
            currentIndex = (currentIndex < 3) ? (currentIndex + 1) : currentIndex;
    
            sLayout->setCurrentIndex(currentIndex);
        }
    
        qDebug() << "NextBtnClicked()";
    }
    
    Widget::~Widget()
    {
        
    }
  • 相关阅读:
    [数据结构]图的DFS和BFS的两种实现方式
    [算法]两个栈实现一个队列
    [数据结构]手动实现队列
    [数据结构]手动实现栈
    [数据结构]手动实现单链表
    Hive分组取Top K数据
    HBase解决海量图片存储方案
    非结构化数据存储方案
    头条面试题之实现两个线程轮流打印字符串
    [算法]最大连续子数组和,最长重复子串,最长无重复字符子串
  • 原文地址:https://www.cnblogs.com/wulei0630/p/6699708.html
Copyright © 2011-2022 走看看