基本对话框--消息框
msgboxdlg.h msgboxdlg.cpp main.cpp 运行图 QustionMsg InFormationMsg WarningMsg CriticalMsg AboutMsg AboutAtMsg 自定义
msgboxdlg.h
# ifndef MSGBOXDLG_H
# define MSGBOXDLG_H
# include <QDialog>
# include <QLabel>
# include <QPushButton>
# include <QGridLayout>
# include <QMessageBox>
class msgboxdlg : public QDialog
{
Q_OBJECT
public :
msgboxdlg ( QWidget * parent = nullptr ) ;
~ msgboxdlg ( ) ;
private slots:
void ShowQustionMsg ( ) ;
void ShowInFormationMsg ( ) ;
void ShowWarningMsg ( ) ;
void ShowCriticalMsg ( ) ;
void ShowAboutMsg ( ) ;
void ShowAboutAtMsg ( ) ;
void ShowCustomMsg ( ) ;
private :
QLabel* Label;
QPushButton* QuestionBtn;
QPushButton* InformationBtn;
QPushButton* WarningBtn;
QPushButton* CriticalBtn;
QPushButton* AboutBtn;
QPushButton* AboutQtBtn;
QPushButton* CustomBtn;
QGridLayout* MainLayout;
} ;
# endif
msgboxdlg.cpp
# include "msgboxdlg.h"
msgboxdlg:: msgboxdlg ( QWidget * parent)
: QDialog ( parent)
{
setWindowTitle ( tr ( "标准消息对话框实例" ) ) ;
Label = new QLabel ( tr ( "请选择一种消息框" ) ) ;
QuestionBtn = new QPushButton ( tr ( "QuestionMsg" ) ) ;
InformationBtn = new QPushButton ( tr ( "InformationMsg" ) ) ;
WarningBtn = new QPushButton ( tr ( "WarningMsg" ) ) ;
CriticalBtn = new QPushButton ( tr ( "CriticalMsg" ) ) ;
AboutBtn = new QPushButton ( tr ( "AboutMsg" ) ) ;
AboutQtBtn = new QPushButton ( tr ( "AboutQtMsg" ) ) ;
CustomBtn = new QPushButton ( tr ( "CustomMsg" ) ) ;
MainLayout = new QGridLayout ( this ) ;
MainLayout-> addWidget ( Label, 0 , 0 , 1 , 2 ) ;
MainLayout-> addWidget ( QuestionBtn, 1 , 0 ) ;
MainLayout-> addWidget ( InformationBtn, 1 , 1 ) ;
MainLayout-> addWidget ( WarningBtn, 2 , 0 ) ;
MainLayout-> addWidget ( CriticalBtn, 2 , 1 ) ;
MainLayout-> addWidget ( AboutBtn, 3 , 0 ) ;
MainLayout-> addWidget ( AboutQtBtn, 3 , 1 ) ;
MainLayout-> addWidget ( CustomBtn, 4 , 0 , 1 , 2 ) ;
connect ( QuestionBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowQustionMsg ( ) ) ) ;
connect ( InformationBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowInFormationMsg ( ) ) ) ;
connect ( WarningBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowWarningMsg ( ) ) ) ;
connect ( CriticalBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowCriticalMsg ( ) ) ) ;
connect ( AboutBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowAboutMsg ( ) ) ) ;
connect ( AboutQtBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowAboutAtMsg ( ) ) ) ;
connect ( CustomBtn, SIGNAL ( clicked ( ) ) , this , SLOT ( ShowCustomMsg ( ) ) ) ;
}
msgboxdlg :: ~ msgboxdlg ( ) { }
void msgboxdlg:: ShowQustionMsg ( )
{
Label-> setText ( tr ( "Question Message box" ) ) ;
switch ( QMessageBox :: question ( this , tr ( "Question 消息框" ) , tr ( "您现在已经修改完成,是否要结束程序?" ) ,
QMessageBox:: Ok| QMessageBox:: Cancel, QMessageBox:: Ok) )
{
case QMessageBox:: Ok:
Label-> setText ( tr ( "Question button/ok" ) ) ;
break ;
case QMessageBox:: Cancel:
Label-> setText ( tr ( "Question button/cancel" ) ) ;
break ;
default :
break ;
}
return ;
}
void msgboxdlg:: ShowInFormationMsg ( )
{
Label-> setText ( tr ( "Information Message Box" ) ) ;
QMessageBox :: information ( this , tr ( "Information消息框" ) , tr ( "这是Information消息框测试,欢迎您!" ) ) ;
return ;
}
void msgboxdlg:: ShowWarningMsg ( )
{
Label-> setText ( tr ( "warning message box" ) ) ;
switch ( QMessageBox :: warning ( this , tr ( "warning 消息框" ) , tr ( "您修改的文档内容还未保存,是否要保存文档" ) , QMessageBox:: Save| QMessageBox:: Discard| QMessageBox:: Cancel, QMessageBox:: Save) )
{
case QMessageBox:: Save:
Label-> setText ( "Save" ) ;
break ;
case QMessageBox:: Discard:
Label-> setText ( "Discard" ) ;
break ;
case QMessageBox:: Cancel:
Label-> setText ( "Cancel" ) ;
break ;
default :
break ;
}
return ;
}
void msgboxdlg:: ShowCriticalMsg ( )
{
Label-> setText ( tr ( "Critical MessBox " ) ) ;
QMessageBox :: critical ( this , tr ( "Critical 消息框" ) , tr ( "这是一个critical 消息框" ) ) ;
return ;
}
void msgboxdlg:: ShowAboutMsg ( )
{
Label-> setText ( tr ( "about MessBox " ) ) ;
QMessageBox :: about ( this , tr ( "about 消息框" ) , tr ( "这是一个about 消息框" ) ) ;
return ;
}
void msgboxdlg:: ShowAboutAtMsg ( )
{
Label-> setText ( tr ( "aboutQt MessBox " ) ) ;
QMessageBox :: aboutQt ( this , tr ( "aboutQt 消息框" ) ) ;
return ;
}
void msgboxdlg:: ShowCustomMsg ( )
{
Label-> setText ( tr ( "Custom 消息框" ) ) ;
QMessageBox CustomBox;
CustomBox. setWindowTitle ( tr ( "自定义消息框" ) ) ;
QPushButton* YesBtn = CustomBox. addButton ( tr ( "yes" ) , QMessageBox:: ActionRole) ;
QPushButton* NoBtn = CustomBox. addButton ( tr ( "No" ) , QMessageBox:: ActionRole) ;
QPushButton* Cancel = CustomBox. addButton ( QMessageBox:: Cancel) ;
CustomBox. setText ( tr ( "这是一个用户自定义消息框" ) ) ;
CustomBox. setIconPixmap ( QPixmap ( "312.png" ) ) ;
CustomBox. exec ( ) ;
if ( CustomBox. clickedButton ( ) == YesBtn)
{
Label-> setText ( tr ( "Custom YesBtn消息" ) ) ;
return ;
}
if ( CustomBox. clickedButton ( ) == NoBtn)
{
Label-> setText ( tr ( "Custom NoBtn消息" ) ) ;
return ;
}
if ( CustomBox. clickedButton ( ) == Cancel)
{
Label-> setText ( tr ( "Custom Cancel消息" ) ) ;
return ;
}
return ;
}
main.cpp
# include "msgboxdlg.h"
# include <QApplication>
int main ( int argc, char * argv[ ] )
{
QApplication a ( argc, argv) ;
msgboxdlg w;
w. show ( ) ;
return a. exec ( ) ;
}
运行图
QustionMsg
InFormationMsg
WarningMsg
CriticalMsg
AboutMsg
AboutAtMsg
自定义