詳解C++ Qt中堆疊窗體的使用案例
更新時間:2023年08月23日 16:06:51 作者:執(zhí)念斬長河
這篇文章主要為大家詳細(xì)介紹了C++ Qt中堆疊窗體的使用案例,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)QT有一定的幫助,感興趣的小伙伴可以了解一下
本博文源于筆者最近學(xué)習(xí)的Qt,內(nèi)容講解堆疊窗體QStackedWidget案例,效果是選擇左側(cè)列表框中不同的選項時,右側(cè)顯示所選的不同的窗體。
案例效果
案例書寫過程
控件都是動態(tài)創(chuàng)建的,因此.h文件需要創(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文件,要分為兩個部分
#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)文章
VSCode 搭建 Arm 遠(yuǎn)程調(diào)試環(huán)境的步驟詳解
這篇文章主要介紹了VSCode 搭建 Arm 遠(yuǎn)程調(diào)試環(huán)境的步驟詳解,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04