国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

深入理解Qt中各種消息框對話框的使用

2020-01-26 14:00:44
字體:
來源:轉載
供稿:網友

最近在學習Qt框架,今天學習了一下消息框的使用, 現整理出來以作記錄。

在程序運行時,經常需要提示用戶一些信息,比如警告啊,提示啊,建議啊之類的東西。這些東西基本上是通過消息框與用戶進行交互的,Qt中主要是用QMessageBox類來加以實現的。

消息框一般分為七種:

  1. Question詢問消息框:為正常的操作提供一個簡單的詢問
  2. Information信息消息框:為正常操作提供一個提示
  3. Warning提示消息框:提醒用戶發生了一個錯誤
  4. Critical警告消息框:警告用戶發生了一個嚴重錯誤
  5. About關于消息框:自定義的關于信息
  6. AboutQt關于Qt消息框:Qt自身的關于信息
  7. Custom自定義消息框:自己定制消息框

具體用法見源碼以及分析:

Dialog.pro

#-------------------------------------------------## Project created by QtCreator 2015-10-24T17:32:35##-------------------------------------------------QT    += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = DialogTEMPLATE = appSOURCES += main.cpp    dialog.cppHEADERS += dialog.h

dialog.h

#ifndefDIALOG_H#defineDIALOG_H#include<QDialog>#include<QGridLayout>#include<QPushButton>#include<QLabel>#include<QMessageBox>class Dialog: public QDialog{  Q_OBJECTpublic:  Dialog(QWidget *parent = 0);  ~Dialog();public://配置部件和布局  QLabel *label;  QPushButton *QuestionBtn,*InformationBtn,*WarningBtn,*CriticalBtn,*AboutBtn,*AboutQtBtn,*CustomBtn;  QGridLayout *layout,*layoutLabel,*layoutBtn;protected slots://各種按鈕的槽  void slotQuestion();  void slotInformation();  void slotWarning();  void slotCritical();  void slotAbout();  void slotAboutQt();  void slotCustom();};#endif// DIALOG_H

dialog.cpp

#include"dialog.h"Dialog::Dialog(QWidget *parent)  : QDialog(parent){  setWindowTitle("QMessageBox");  QuestionBtn=new QPushButton("Question");  InformationBtn=new QPushButton("Information");  WarningBtn=new QPushButton("Warning");  CriticalBtn=new QPushButton("Critical");  AboutBtn=new QPushButton("About");  AboutQtBtn=new QPushButton("AboutQt");  CustomBtn=new QPushButton("Custom");  label=new QLabel("About Qt MessageBox:");  layout=new QGridLayout(this);  layoutLabel=new QGridLayout;  layoutBtn=new QGridLayout;  layoutLabel->addWidget(label,0,0);  layoutBtn->addWidget(QuestionBtn,1,0);  layoutBtn->addWidget(InformationBtn,1,1);  layoutBtn->addWidget(WarningBtn,2,0);  layoutBtn->addWidget(CriticalBtn,2,1);  layoutBtn->addWidget(AboutBtn,3,0);  layoutBtn->addWidget(AboutQtBtn,3,1);  layoutBtn->addWidget(CustomBtn,4,0);  layoutBtn->setSpacing(15);  //嵌套布局  layout->addLayout(layoutLabel,0,0);  layout->addLayout(layoutBtn,1,0);  setFixedSize(300,220);//固定大小  connect(QuestionBtn,SIGNAL(clicked()),this,SLOT(slotQuestion()));  connect(InformationBtn,SIGNAL(clicked()),this,SLOT(slotInformation()));  connect(WarningBtn,SIGNAL(clicked()),this,SLOT(slotWarning()));  connect(CriticalBtn,SIGNAL(clicked()),this,SLOT(slotCritical()));  connect(AboutBtn,SIGNAL(clicked()),this,SLOT(slotAbout()));  connect(AboutQtBtn,SIGNAL(clicked()),this,SLOT(slotAboutQt()));  connect(CustomBtn,SIGNAL(clicked()),this,SLOT(slotCustom()));}Dialog::~Dialog(){}//直接調用AboutQt,設置句柄和標題即可void Dialog::slotAboutQt(){ QMessageBox::aboutQt(this,"This is the title");}//以下三個函數均是設置句柄標題和信息即可,也可以在最后設置默認按鈕,一般默認的是QMessageBox::Ok。void Dialog::slotAbout(){   QMessageBox::about(this,"About","This is the label.");}void Dialog::slotCritical(){  QMessageBox::critical(this,"Critical","This is the label.");}void Dialog::slotInformation(){ QMessageBox::information(this,"Information","This is the label.");}//自定義消息框void Dialog::slotCustom(){  QMessageBox customMsgBox;  customMsgBox.setWindowTitle("Custom message box");  //添加按鍵  QPushButton *lockBtn=customMsgBox.addButton("Lock",QMessageBox::ActionRole);  QPushButton *unlockBtn=customMsgBox.addButton("Unlock",QMessageBox::ActionRole);  QPushButton *cancelBtn=customMsgBox.addButton(QMessageBox::Cancel);//注意cancel不能指定Text  //customMsgBox.setIconPixmap(QPixmap("a.png"));//設置圖片  customMsgBox.setText("This is the label");  customMsgBox.exec();//執行消息框  QPushButton *msg=(QPushButton*)customMsgBox.clickedButton();//接受按鍵信息  //判斷按鍵  if(msg==lockBtn)    label->setText("Custom button /lock");  if(msg==unlockBtn)    label->setText("Custom button /unlock");  if(msg==cancelBtn)    label->setText("Custom button /cancel");}void Dialog::slotQuestion(){  //QMessageBox::**question()**函數,傳入句柄,標題,文本,按鈕值,返回按鍵對應的值,最后也可以加默認按鍵的位置  int msg=QMessageBox::question(this,"Question","This is the label.",QMessageBox::Ok|QMessageBox::Cancel);  //判斷選擇信息  switch(msg){  case QMessageBox::Ok:    label->setText("Question button /OK");    break;  case QMessageBox::Cancel:    label->setText("Question button /Cancel");    break;  default:    break;  }}void Dialog::slotWarning(){  //QmessageBox::warning()函數同Question函數  int msg=QMessageBox::warning(this,"Question","This is the label.",QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save);  switch(msg){//判斷選擇信息  case QMessageBox::Save:    label->setText("Warning button /Save");    break;  case QMessageBox::Cancel:    label->setText("Warning button /Cancel");    break;  case QMessageBox::Discard:    label->setText("Warning button /Discard");    break;  default:    break;  }}

##main.cpp

#include"dialog.h"#include<QApplication>int main(intargc,char*argv[]){  QApplicationa(argc, argv);  Dialog w;  w.show();  return a.exec();}

運行截圖

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 乐陵市| 洪泽县| 蓝田县| 邮箱| 沐川县| 泽普县| 辉南县| 项城市| 清河县| 扶风县| 察隅县| 蕲春县| 潼关县| 安达市| 永靖县| 新乐市| 敦化市| 南华县| 青河县| 墨竹工卡县| 夹江县| 马边| 丽水市| 丰城市| 建宁县| 五大连池市| 东丽区| 通州区| 来凤县| 长子县| 苍梧县| 东平县| 滦平县| 延津县| 吴江市| 项城市| 宁国市| 六安市| 绥德县| 共和县| 谢通门县|