利用Qt實現可擴展對話框的示例代碼
可擴展對話框通常用于用戶對界面有不同要求的場合。通常情況下,只出現在基本對話窗體;當供高級用戶使用或需要更多信息時,可通過某種方式的切換顯示完整對話窗體(擴展窗體)。切換的工作通常由一個按鈕完成。
一、項目介紹
實現一個簡單填寫資料的對話框。通常情況下,只需要填寫姓名和性別。若有特殊需要,還需要填寫更多信息時,則切換至完整對話窗體,完整對話窗體包括年齡、部門、聯(lián)系方式等。此時詳細按鈕切換為簡略按鈕,在此點擊縮回簡單對話窗體。
二、項目基本配置
新建一個Qt案例,項目名稱為“Extension”,基類選擇“QDialog”,取消創(chuàng)建UI界面復選框的選中狀態(tài),完成項目創(chuàng)建。
三、UI界面設計
無UI界面
四、主程序實現
4.1 dialog.h頭文件
聲明一個按鈕點擊槽函數:
private slots: void showDetailInfo();
聲明兩個函數實現窗體部分的創(chuàng)建,聲明兩個窗體和一個按鈕:
private: void createBaseInfo(); //實現基本對話窗體部分 void createDetailInfo();//實現擴展窗體部分 QWidget *baseWidget; //基本窗體部分 QWidget *detailWidget; //擴展窗體部分 QPushButton *DetailBtn; //詳細-簡略按鈕
4.2 dialog.cpp源文件
主函數定義如下:
setWindowTitle("Extension Dialog"); //窗體標題 createBaseInfo(); createDetailInfo(); QVBoxLayout *layout=new QVBoxLayout(this);//布局 layout->addWidget(baseWidget); layout->addWidget(detailWidget); layout->setSizeConstraint(QLayout::SetFixedSize);//設置窗體的大小固定,不能利用拖拽改變大小 layout->setSpacing(10); //設置間距為10
定義createBaseInfo()函數,完成基本窗體baseWidget的構建:
//完成基本窗體baseWidget的構建 void Dialog::createBaseInfo() { baseWidget=new QWidget; QLabel *nameLabel=new QLabel("姓名:"); QLineEdit *nameLineEdit=new QLineEdit; QLabel *sexLabel=new QLabel("性別:"); QComboBox *sexComboBox=new QComboBox; sexComboBox->insertItem(0,"女"); sexComboBox->insertItem(1,"男"); QGridLayout *LeftLayout=new QGridLayout; LeftLayout->addWidget(nameLabel,0,0); LeftLayout->addWidget(nameLineEdit,0,1); LeftLayout->addWidget(sexLabel,1,0); LeftLayout->addWidget(sexComboBox,1,1); QPushButton *OKBtn=new QPushButton("確定"); DetailBtn=new QPushButton("詳細"); QDialogButtonBox *btnBox=new QDialogButtonBox(Qt::Vertical); btnBox->addButton(OKBtn,QDialogButtonBox::ActionRole); btnBox->addButton(DetailBtn,QDialogButtonBox::ActionRole); QHBoxLayout *mainLayout=new QHBoxLayout(baseWidget); mainLayout->addLayout(LeftLayout); mainLayout->addWidget(btnBox); connect(DetailBtn,SIGNAL(clicked()),this,SLOT(showDetailInfo()));//點擊詳細按鈕觸發(fā)showDetailInfo()槽函數 }
定義createDetailInfo()函數,完成擴展窗體detailWidget的構建:
//實現擴展窗體detailWidget的構建 void Dialog::createDetailInfo() { detailWidget=new QWidget; QLabel *ageLabel=new QLabel("年齡:"); QLineEdit *ageLineEdit=new QLineEdit; ageLineEdit->setText("30"); QLabel *departmentLabel=new QLabel("部門:"); QComboBox *departmentComboBox=new QComboBox; departmentComboBox->addItem("部門1"); departmentComboBox->addItem("部門2"); departmentComboBox->addItem("部門3"); QLabel *teleLabel=new QLabel("電話:"); QLineEdit *teleLineEdit=new QLineEdit; QGridLayout *mainLayout=new QGridLayout(detailWidget); mainLayout->addWidget(ageLabel,0,0); mainLayout->addWidget(ageLineEdit,0,1); mainLayout->addWidget(departmentLabel,1,0); mainLayout->addWidget(departmentComboBox,1,1); mainLayout->addWidget(teleLabel,2,0); mainLayout->addWidget(teleLineEdit,2,1); detailWidget->hide();//隱藏詳細窗體 }
最后實現窗體和按鈕文本的切換功能:
//實現窗體的擴展切換功能 void Dialog::showDetailInfo(){ if(detailWidget->isHidden()){ detailWidget->show(); DetailBtn->setText("簡略"); } else { detailWidget->hide(); DetailBtn->setText("詳細"); } }
五、效果演示
到此這篇關于利用Qt實現可擴展對話框的示例代碼的文章就介紹到這了,更多相關Qt可擴展對話框內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++下如何將TensorFlow模型封裝成DLL供C#調用
這篇文章主要介紹了C++下如何將TensorFlow模型封裝成DLL供C#調用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11