当前位置:网站首页>QT learning 26 integrated example of layout management

QT learning 26 integrated example of layout management

2022-07-07 07:59:00 A little black sauce

Qt Study 26 Comprehensive example of layout management

Demand analysis

  • Practice developing a wizard user interface
    • Show on the same interface Different wizard pages
    • adopt “ The previous step ” and “ next step ” Button to switch
    • The element components and component layout on different pages are different
    • The components in the page are arranged through the layout manager

Solution

  • Interface design through layout nesting

 Insert picture description here

  • adopt QStackedLayout Manage different pages

 Insert picture description here

  • Generate different pages through sub components

 Insert picture description here

Programming experiment - Wizard user interface

#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
#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);
}
#include "Widget.h"
#include <QApplication>

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

    return a.exec();
}

matters needing attention

  • Container of any component You can specify the layout manager
  • Components in the same layout manager have the same parent component
  • While setting layout management Implicit designated parent-child relationship

 Insert picture description here

  • In the figure Components 1 and Components 2 Managed by the same layout manager , Have the same parent component .

Summary

  • The layout manager can Nested with each other to form a complex user interface
  • Any container component can set the layout manager
  • Components in the same layout manager have the same parent component
  • The parent-child relationship between components is Qt An important way of memory management in
原网站

版权声明
本文为[A little black sauce]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070431162158.html