深入理解Qt中各種消息框?qū)υ捒虻氖褂?/h1>
      來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次
      
        
									
最近在學(xué)習(xí)Qt框架,今天學(xué)習(xí)了一下消息框的使用, 現(xiàn)整理出來以作記錄。
在程序運(yùn)行時(shí),經(jīng)常需要提示用戶一些信息,比如警告啊,提示啊,建議啊之類的東西。這些東西基本上是通過消息框與用戶進(jìn)行交互的,Qt中主要是用QMessageBox類來加以實(shí)現(xiàn)的。
消息框一般分為七種:
  - Question詢問消息框:為正常的操作提供一個(gè)簡單的詢問
 
  - Information信息消息框:為正常操作提供一個(gè)提示
 
  - Warning提示消息框:提醒用戶發(fā)生了一個(gè)錯(cuò)誤
 
  - Critical警告消息框:警告用戶發(fā)生了一個(gè)嚴(yán)重錯(cuò)誤
 
  - About關(guān)于消息框:自定義的關(guān)于信息
 
  - AboutQt關(guān)于Qt消息框:Qt自身的關(guān)于信息
 
  - Custom自定義消息框:自己定制消息框
 
具體用法見源碼以及分析:
Dialog.pro
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-24T17:32:35
#
#-------------------------------------------------
QT    += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Dialog
TEMPLATE = app
SOURCES += main.cpp
    dialog.cpp
HEADERS += dialog.h
dialog.h
#ifndefDIALOG_H
#defineDIALOG_H
#include<QDialog>
#include<QGridLayout>
#include<QPushButton>
#include<QLabel>
#include<QMessageBox>
class Dialog: public QDialog
{
  Q_OBJECT
public:
  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()
{
}
//直接調(diào)用AboutQt,設(shè)置句柄和標(biāo)題即可
void Dialog::slotAboutQt(){
 QMessageBox::aboutQt(this,"This is the title");
}
//以下三個(gè)函數(shù)均是設(shè)置句柄標(biāo)題和信息即可,也可以在最后設(shè)置默認(rèn)按鈕,一般默認(rèn)的是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"));//設(shè)置圖片
  customMsgBox.setText("This is the label");
  customMsgBox.exec();//執(zhí)行消息框
  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()**函數(shù),傳入句柄,標(biāo)題,文本,按鈕值,返回按鍵對應(yīng)的值,最后也可以加默認(rèn)按鍵的位置
  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()函數(shù)同Question函數(shù)
  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();
}
運(yùn)行截圖
      
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
									
        
      
      
    
最近在學(xué)習(xí)Qt框架,今天學(xué)習(xí)了一下消息框的使用, 現(xiàn)整理出來以作記錄。
在程序運(yùn)行時(shí),經(jīng)常需要提示用戶一些信息,比如警告啊,提示啊,建議啊之類的東西。這些東西基本上是通過消息框與用戶進(jìn)行交互的,Qt中主要是用QMessageBox類來加以實(shí)現(xiàn)的。
消息框一般分為七種:
- Question詢問消息框:為正常的操作提供一個(gè)簡單的詢問
 - Information信息消息框:為正常操作提供一個(gè)提示
 - Warning提示消息框:提醒用戶發(fā)生了一個(gè)錯(cuò)誤
 - Critical警告消息框:警告用戶發(fā)生了一個(gè)嚴(yán)重錯(cuò)誤
 - About關(guān)于消息框:自定義的關(guān)于信息
 - AboutQt關(guān)于Qt消息框:Qt自身的關(guān)于信息
 - Custom自定義消息框:自己定制消息框
 
具體用法見源碼以及分析:
Dialog.pro
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-24T17:32:35
#
#-------------------------------------------------
QT    += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Dialog
TEMPLATE = app
SOURCES += main.cpp
    dialog.cpp
HEADERS += dialog.h
dialog.h
#ifndefDIALOG_H
#defineDIALOG_H
#include<QDialog>
#include<QGridLayout>
#include<QPushButton>
#include<QLabel>
#include<QMessageBox>
class Dialog: public QDialog
{
  Q_OBJECT
public:
  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()
{
}
//直接調(diào)用AboutQt,設(shè)置句柄和標(biāo)題即可
void Dialog::slotAboutQt(){
 QMessageBox::aboutQt(this,"This is the title");
}
//以下三個(gè)函數(shù)均是設(shè)置句柄標(biāo)題和信息即可,也可以在最后設(shè)置默認(rèn)按鈕,一般默認(rèn)的是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"));//設(shè)置圖片
  customMsgBox.setText("This is the label");
  customMsgBox.exec();//執(zhí)行消息框
  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()**函數(shù),傳入句柄,標(biāo)題,文本,按鈕值,返回按鍵對應(yīng)的值,最后也可以加默認(rèn)按鍵的位置
  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()函數(shù)同Question函數(shù)
  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();
}
運(yùn)行截圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語言
下一篇:C++標(biāo)準(zhǔn)庫bitset類型的簡單使用方法介紹
本文標(biāo)題:深入理解Qt中各種消息框?qū)υ捒虻氖褂?/a>
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1349.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
 - 01-10深入理解約瑟夫環(huán)的數(shù)學(xué)優(yōu)化方法
 - 01-10深入二叉樹兩個(gè)結(jié)點(diǎn)的最低共同父結(jié)點(diǎn)的詳解
 - 01-10深入理解C++中常見的關(guān)鍵字含義
 - 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
 - 01-10APUE筆記之:進(jìn)程環(huán)境詳解
 - 01-10深入第K大數(shù)問題以及算法概要的詳解
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 04-02jquery與jsp,用jquery
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 


