深入理解Qt中各種消息框?qū)υ捒虻氖褂?/h1>
更新時(shí)間:2017年07月19日 16:28:15 作者:Myths
本篇文章主要介紹了Qt中各種消息框的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
最近在學(xué)習(xí)Qt框架,今天學(xué)習(xí)了一下消息框的使用, 現(xiàn)整理出來(lái)以作記錄。
在程序運(yùn)行時(shí),經(jīng)常需要提示用戶一些信息,比如警告啊,提示啊,建議啊之類的東西。這些東西基本上是通過(guò)消息框與用戶進(jìn)行交互的,Qt中主要是用QMessageBox類來(lái)加以實(shí)現(xiàn)的。
消息框一般分為七種:
- Question詢問(wèn)消息框:為正常的操作提供一個(gè)簡(jiǎn)單的詢問(wèn)
- 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自定義消息框:自己定制消息框
具體用法見(jiàn)源碼以及分析:
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)題,文本,按鈕值,返回按鍵對(duì)應(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)行截圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Qt圖形圖像開(kāi)發(fā)之曲線圖模塊QCustomplot庫(kù)生成靜態(tài)、動(dòng)態(tài)曲線詳細(xì)教程圖解
- Qt圖形圖像開(kāi)發(fā)之高性能曲線圖模塊QCustomplot庫(kù)詳細(xì)使用方法與實(shí)例(支持動(dòng)、靜曲線圖)
- Qt學(xué)習(xí)教程之對(duì)話框消失動(dòng)畫效果
- Qt學(xué)習(xí)教程之表格控件螞蟻線詳解
- Qt 使用Poppler實(shí)現(xiàn)pdf閱讀器的示例代碼
- Qt實(shí)現(xiàn)保存、瀏覽、預(yù)覽、打印功能的示例代碼
- QT開(kāi)發(fā)應(yīng)用程序的歡迎界面實(shí)例
- QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例
- 在QT5中實(shí)現(xiàn)求兩個(gè)輸入值的和并輸出(實(shí)例)
- QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)
- 淺談Qt中使用CEF的幾個(gè)要點(diǎn)(Windows下)
- Qt實(shí)現(xiàn)FTP的上傳和下載的實(shí)例代碼
- Qt如何設(shè)置窗口屏幕居中顯示以及設(shè)置大小
- Qt圖形圖像開(kāi)發(fā)之曲線圖表庫(kù)QChart編譯安裝詳細(xì)方法與使用實(shí)例
相關(guān)文章
-
vc中float與DWORD的互想轉(zhuǎn)換實(shí)現(xiàn)代碼
這篇文章主要介紹了vc中float與DWORD的互想轉(zhuǎn)換實(shí)現(xiàn)代碼,需要的朋友可以參考下 2017-06-06
-
C++實(shí)現(xiàn)校園運(yùn)動(dòng)會(huì)報(bào)名系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)校園運(yùn)動(dòng)會(huì)報(bào)名系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2018-10-10
-
C語(yǔ)言深入探索浮點(diǎn)數(shù)的使用秘密
在C語(yǔ)言中,浮點(diǎn)數(shù)是一個(gè)很重要的類型,浮點(diǎn)數(shù)可以使數(shù)據(jù)更為精確。浮點(diǎn)數(shù)說(shuō)白了就是帶有小數(shù)點(diǎn)的數(shù)。比如1.6?0.0000?765.2等等,浮點(diǎn)數(shù)具體是怎么用的呢,讓我們一起來(lái)看看 2022-04-04
-
C語(yǔ)言中fchdir()函數(shù)和rewinddir()函數(shù)的使用詳解
這篇文章主要介紹了C語(yǔ)言中fchdir()函數(shù)和rewinddir()函數(shù)的使用詳解,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下 2015-09-09
-
C語(yǔ)言實(shí)現(xiàn)酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2022-06-06
-
C語(yǔ)言中isalnum()函數(shù)和isalpha()函數(shù)的對(duì)比使用
這篇文章主要介紹了C語(yǔ)言中isalnum()函數(shù)和isalpha()函數(shù)的對(duì)比使用,都可以判斷是否為字母但isalnum的判斷還包括數(shù)字,需要的朋友可以參考下 2015-08-08
-
C++實(shí)現(xiàn)十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制整數(shù)的方法
這篇文章主要介紹了C++實(shí)現(xiàn)十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制整數(shù)的方法,涉及C++字符串與數(shù)制轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 2015-07-07
最新評(píng)論
最近在學(xué)習(xí)Qt框架,今天學(xué)習(xí)了一下消息框的使用, 現(xiàn)整理出來(lái)以作記錄。
在程序運(yùn)行時(shí),經(jīng)常需要提示用戶一些信息,比如警告啊,提示啊,建議啊之類的東西。這些東西基本上是通過(guò)消息框與用戶進(jìn)行交互的,Qt中主要是用QMessageBox類來(lái)加以實(shí)現(xiàn)的。
消息框一般分為七種:
- Question詢問(wèn)消息框:為正常的操作提供一個(gè)簡(jiǎn)單的詢問(wèn)
- 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自定義消息框:自己定制消息框
具體用法見(jiàn)源碼以及分析:
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)題,文本,按鈕值,返回按鍵對(duì)應(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)行截圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Qt圖形圖像開(kāi)發(fā)之曲線圖模塊QCustomplot庫(kù)生成靜態(tài)、動(dòng)態(tài)曲線詳細(xì)教程圖解
- Qt圖形圖像開(kāi)發(fā)之高性能曲線圖模塊QCustomplot庫(kù)詳細(xì)使用方法與實(shí)例(支持動(dòng)、靜曲線圖)
- Qt學(xué)習(xí)教程之對(duì)話框消失動(dòng)畫效果
- Qt學(xué)習(xí)教程之表格控件螞蟻線詳解
- Qt 使用Poppler實(shí)現(xiàn)pdf閱讀器的示例代碼
- Qt實(shí)現(xiàn)保存、瀏覽、預(yù)覽、打印功能的示例代碼
- QT開(kāi)發(fā)應(yīng)用程序的歡迎界面實(shí)例
- QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例
- 在QT5中實(shí)現(xiàn)求兩個(gè)輸入值的和并輸出(實(shí)例)
- QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)
- 淺談Qt中使用CEF的幾個(gè)要點(diǎn)(Windows下)
- Qt實(shí)現(xiàn)FTP的上傳和下載的實(shí)例代碼
- Qt如何設(shè)置窗口屏幕居中顯示以及設(shè)置大小
- Qt圖形圖像開(kāi)發(fā)之曲線圖表庫(kù)QChart編譯安裝詳細(xì)方法與使用實(shí)例
相關(guān)文章
vc中float與DWORD的互想轉(zhuǎn)換實(shí)現(xiàn)代碼
這篇文章主要介紹了vc中float與DWORD的互想轉(zhuǎn)換實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-06-06
C++實(shí)現(xiàn)校園運(yùn)動(dòng)會(huì)報(bào)名系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)校園運(yùn)動(dòng)會(huì)報(bào)名系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
C語(yǔ)言深入探索浮點(diǎn)數(shù)的使用秘密
在C語(yǔ)言中,浮點(diǎn)數(shù)是一個(gè)很重要的類型,浮點(diǎn)數(shù)可以使數(shù)據(jù)更為精確。浮點(diǎn)數(shù)說(shuō)白了就是帶有小數(shù)點(diǎn)的數(shù)。比如1.6?0.0000?765.2等等,浮點(diǎn)數(shù)具體是怎么用的呢,讓我們一起來(lái)看看2022-04-04
C語(yǔ)言中fchdir()函數(shù)和rewinddir()函數(shù)的使用詳解
這篇文章主要介紹了C語(yǔ)言中fchdir()函數(shù)和rewinddir()函數(shù)的使用詳解,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C語(yǔ)言實(shí)現(xiàn)酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C語(yǔ)言中isalnum()函數(shù)和isalpha()函數(shù)的對(duì)比使用
這篇文章主要介紹了C語(yǔ)言中isalnum()函數(shù)和isalpha()函數(shù)的對(duì)比使用,都可以判斷是否為字母但isalnum的判斷還包括數(shù)字,需要的朋友可以參考下2015-08-08
C++實(shí)現(xiàn)十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制整數(shù)的方法
這篇文章主要介紹了C++實(shí)現(xiàn)十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制整數(shù)的方法,涉及C++字符串與數(shù)制轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

