詳解C++ Qt中堆疊窗體的使用案例
本博文源于筆者最近學(xué)習(xí)的Qt,內(nèi)容講解堆疊窗體QStackedWidget案例,效果是選擇左側(cè)列表框中不同的選項(xiàng)時(shí),右側(cè)顯示所選的不同的窗體。
案例效果
案例書寫過程
控件都是動(dòng)態(tài)創(chuàng)建的,因此.h文件需要?jiǎng)?chuàng)建控件,.cpp書寫業(yè)務(wù)代碼
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include<QListWidget> #include<QStackedWidget> #include<QLabel> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = nullptr); ~Dialog(); private: Ui::Dialog *ui; QListWidget *list; QStackedWidget *stack; QLabel* label1; QLabel* label2; QLabel* label3; }; #endif // DIALOG_H
.cpp文件,要分為兩個(gè)部分
#include "dialog.h" #include "ui_dialog.h" #include<QHBoxLayout> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { // ui->setupUi(this); setWindowTitle(tr("StackedWidget")); list = new QListWidget(this); list->insertItem(0,tr("Window1")); list->insertItem(1,tr("Window2")); list->insertItem(2,tr("Window3")); label1 = new QLabel(tr("WindowTest1")); label2 = new QLabel(tr("WindowTest2")); label3 = new QLabel(tr("WindowTest3")); stack = new QStackedWidget(this); // stack->addWidget(label1); stack->addWidget(label2); stack->addWidget(label3); QHBoxLayout* mainLayout = new QHBoxLayout(this); mainLayout->setMargin(5); mainLayout->setSpacing(5); mainLayout->addWidget(list); mainLayout->addWidget(stack,0,Qt::AlignHCenter); mainLayout->setStretchFactor(list,1); mainLayout->setStretchFactor(stack,3); connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int))); } Dialog::~Dialog() { delete ui; }
到此這篇關(guān)于詳解C++ Qt中堆疊窗體的使用案例的文章就介紹到這了,更多相關(guān)Qt堆疊窗體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于C語言實(shí)現(xiàn)計(jì)算生辰八字五行的示例詳解
生辰八字,簡稱八字,是指一個(gè)人出生時(shí)的干支歷日期;年月日時(shí)共四柱干支,每柱兩字,合共八個(gè)字。這篇文章主要介紹了C語言實(shí)現(xiàn)計(jì)算生辰八字五行的示例代碼,需要的可以參考一下2023-03-03C++中auto類型說明符詳解(附易錯(cuò)實(shí)例)
這篇文章主要給大家介紹了關(guān)于C++中auto類型說明符的相關(guān)資料,文中還附易錯(cuò)實(shí)例,在C++11中引入了auto類型說明符,用它就能讓編譯器替我們?nèi)シ治霰磉_(dá)式所屬的類型,需要的朋友可以參考下2023-07-07VSCode 搭建 Arm 遠(yuǎn)程調(diào)試環(huán)境的步驟詳解
這篇文章主要介紹了VSCode 搭建 Arm 遠(yuǎn)程調(diào)試環(huán)境的步驟詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04