.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
//此类继承QDialog
//类内创建需要用到的变量,函数等,在构造函数初始化中填充数据
class MessageBoxMy:public QDialog
{
Q_OBJECT
public:
//构造函数
MessageBoxMy();
public:
//按键对象创建
QLabel *label1;
QPushButton *questionButton; //为正常的操作提示一个简单的询问
QPushButton *informationButton; //为正常操作提示一个提示
QPushButton *warningButton; //提醒用户啊发生了一个错误
QPushButton *criticalButton; //警告用户发生了一个严重错误
QPushButton *aboutButton; //关于消息框
QPushButton *aboutqtButton; //关于QT消息框
QPushButton *customButton; //自定义消息框
private slots:
void slotQuestion();
void slotInformation();
void slotWorning();
void slotCritical();
void slotAbout();
void slotAboutQt();
void slotCustom();
};
#endif // MYCLASS_H
.c
#include"myclass.h"
#include <QMessageBox>
#include <QGridLayout>
#include <QButtonGroup>
MessageBoxMy::MessageBoxMy()
{
//设置主题
setWindowTitle("MessageBox Use");
label1 = new QLabel("About QT MessageBox");
//创建按键并将文本显示在按键上
questionButton = new QPushButton("Quessage");
informationButton = new QPushButton("Information");
warningButton = new QPushButton("warning");
criticalButton = new QPushButton("critical");
aboutButton = new QPushButton("about");
aboutqtButton = new QPushButton("about qt");
customButton = new QPushButton("custom");
//布局管理
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(label1,0,0,1,2);//放在第0行0列,跨度为1行2列(占一行两列)
layout->addWidget(questionButton,1,0);
layout->addWidget(informationButton,1,1);
layout->addWidget(warningButton,2,0);
layout->addWidget(criticalButton,2,1);
layout->addWidget(aboutButton,3,0);
layout->addWidget(aboutqtButton,3,1);
layout->addWidget(customButton,4,0);
//信号与槽连接
connect(questionButton,SIGNAL(clicked()),this,SLOT(slotQuestion()));
connect(informationButton,SIGNAL(clicked()),this,SLOT(slotInformation()));
connect(warningButton,SIGNAL(clicked()),this,SLOT(slotWorning()));
connect(criticalButton,SIGNAL(clicked()),this,SLOT(slotCritical()));
connect(aboutButton,SIGNAL(clicked()),this,SLOT(slotAbout()));
connect(aboutqtButton,SIGNAL(clicked()),this,SLOT(slotAboutQt()));
connect(customButton,SIGNAL(clicked()),this,SLOT(slotCustom()));
}
//曹函数实现
//对于question消息框,调用时直接使用QMessageBox::question()即可
//第一个参数为消息框的父窗口指针
//第二个参数为消息框的标题栏
//第三个参数为消息框的问题提示
//第四个参数为对消息框按钮的设定QMessageBox提供了很多的标准按钮直接调用,用“|”连写,默认为OK
//第五个参数为消息框出现时,焦点默认在哪个按钮上
void MessageBoxMy::slotQuestion()
{
switch(QMessageBox::question(this,"Question","已到达文档结尾,是否从头查找?",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok))
{
case QMessageBox::Ok:
label1->setText("Question Button/OK");
break;
case QMessageBox::Cancel:
label1->setText("Question Button/Cancel");
break;
default:
break;
}
return;
}
//QMessageBox::information()
//第一个参数为消息框的父窗口指针
//第二个参数为消息框的标题栏
//第三个参数为消息框的问题提示
//后面两个参数与Qusetion用法一样,但在使用中,经常省略,直接使用默认的OK
//Qusetion()和information()可以通用,使用Qusetion的地方都可以使用information替换
void MessageBoxMy::slotInformation()
{
QMessageBox::information(this,"Information","填写任意想告诉于用户的信息!");
return;
}
//MessageBoxMy::slotWorning()
//用法都大差不差
void MessageBoxMy::slotWorning()
{
switch(QMessageBox::warning(this,"warning","是否保存对文档的修改",
QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save))
{
case QMessageBox::Save:
label1->setText("warning button/save");
break;
case QMessageBox::Discard:
label1->setText("warning button/discard");
break;
case QMessageBox::Cancel:
label1->setText("warning button/cancel");
break;
default:
break;
}
return;
}
//Critical()消息框是在系统出现严重错误的时候对用户进行提醒
//通常只填前三个参数
void MessageBoxMy::slotCritical()
{
QMessageBox::critical(this,"critical","致命错误");
label1->setText("Critical Message!");
return;
}
//提供系统的版本信息,只需提供信息而不需要用户反馈信息,只需填写前三个参数即可
void MessageBoxMy::slotAbout()
{
QMessageBox::about(this,"About","测试试验");
label1->setText("About test!");
return;
}
//About qt消息框是QT预定好的消息框,用于提供QT的相关信息
//只需提供两个参数即可,显示内容是QT自己预定好的
void MessageBoxMy::slotAboutQt()
{
QMessageBox::aboutQt(this,"AboutQT");
label1->setText("AboutQT test!");
return;
}
//
void MessageBoxMy::slotCustom()
{
QMessageBox customMsgBox;//创建一个QMessageBox对象
customMsgBox.setWindowTitle("Custom message box!!!");//设置标题栏
//第一个参数为按钮显示的文字,第二个参数为按钮类型的描述
QPushButton *lookbutton = customMsgBox.addButton("锁定",QMessageBox::ActionRole);//在窗口上添加按键并让按键显示固定文字
QPushButton *unlookbutton = customMsgBox.addButton("解锁",QMessageBox::ActionRole);
//取消按键只需要一个参数
QPushButton *cancelbutton = customMsgBox.addButton(QMessageBox::Cancel);
//customMsgBox.setIconPixmap(QPixmap(":/图片路径"));
customMsgBox.setText("这是一个自定义的消息框");
customMsgBox.exec();//进入消息循环,等待可能输入进行响应
//判断按键按下显示对应字符
if(customMsgBox.clickedButton() == lookbutton)
label1->setText("LOOK");
if(customMsgBox.clickedButton() == unlookbutton)
label1->setText("UNlook");
if(customMsgBox.clickedButton() == cancelbutton)
label1->setText("cancel");
return;
}
main.c
#include "mainwindow.h"
#include <QApplication>
#include "myclass.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MessageBoxMy b;
b.show();
return a.exec();
}