博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QT中各种MessageBox的使用
阅读量:5981 次
发布时间:2019-06-20

本文共 4371 字,大约阅读时间需要 14 分钟。

MessageBox.h

#ifndef MESSAGEBOX_H#define MESSAGEBOX_H#include 
#include "ui_messagebox.h"class MessageBox : public QDialog{ Q_OBJECTpublic: MessageBox(QWidget *parent = 0, Qt::WFlags flags = 0); ~MessageBox();private: Ui::MessageBoxClass ui; QLabel *label; private slots: void slotQuestion(); void slotInformation(); void slotWarning(); void slotCritical(); void slotAbout(); void slotAboutQt(); void slotCustom();};#endif // MESSAGEBOX_H

MessageBox.cpp

#include "messagebox.h"MessageBox::MessageBox(QWidget *parent, Qt::WFlags flags)	: QDialog(parent, flags){	ui.setupUi(this);	setWindowTitle(tr("Message Box Example"));	label = new QLabel;	QPushButton *btn1 = new QPushButton("Question");	QPushButton *btn2 = new QPushButton("Information");	QPushButton *btn3 = new QPushButton("Warning");	QPushButton *btn4 = new QPushButton("Critical");	QPushButton *btn5 = new QPushButton("About");	QPushButton *btn6 = new QPushButton("About Qt");	QPushButton *btn7 = new QPushButton("Custom");	QGridLayout *grid = new QGridLayout;	grid->addWidget(btn1,0,0);	grid->addWidget(btn2,0,1);	grid->addWidget(btn3,1,0);	grid->addWidget(btn4,1,1);	grid->addWidget(btn5,2,0);	grid->addWidget(btn6,2,1);	grid->addWidget(btn7,3,0);	QVBoxLayout *mainLayout = new QVBoxLayout;	mainLayout->setMargin(10);	mainLayout->setSpacing(20);	mainLayout->addWidget(label);	mainLayout->addLayout(grid);	setLayout(mainLayout);	connect(btn1,SIGNAL(clicked()),this,SLOT(slotQuestion()));	connect(btn2,SIGNAL(clicked()),this,SLOT(slotInformation()));	connect(btn3,SIGNAL(clicked()),this,SLOT(slotWarning()));	connect(btn4,SIGNAL(clicked()),this,SLOT(slotCritical()));	connect(btn5,SIGNAL(clicked()),this,SLOT(slotAbout()));	connect(btn6,SIGNAL(clicked()),this,SLOT(slotAboutQt()));	connect(btn7,SIGNAL(clicked()),this,SLOT(slotCustom()));}MessageBox::~MessageBox(){}void MessageBox::slotQuestion(){	switch(QMessageBox::question(this,"Question",tr("It's end of document,search from begin?"),		QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok))	{	case QMessageBox::Ok:		label->setText(" Question button / Ok ");		break;	case QMessageBox::Cancel:		label->setText(" Question button / Cancel ");		break;	default:		break;	}	return;}void MessageBox::slotInformation(){	QMessageBox::information(this,"Information",tr("anything you want tell user"));	return;}void MessageBox::slotWarning(){	switch(QMessageBox::warning(this,"Warning",tr("Save changes to document?"),		QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save))	{	case QMessageBox::Save:		label->setText(" Warning button / Save ");		break;	case QMessageBox::Discard:		label->setText(" Warning button / Discard ");		break;	case QMessageBox::Cancel:		label->setText(" Warning button / Cancel ");		break;	default:		break;	}	return;}void MessageBox::slotCritical(){	QMessageBox::critical(this,"Critical",tr("tell user a critical error"));	label->setText(" Critical MessageBox ");	return;}void MessageBox::slotAbout(){	QMessageBox::about(this,"About",tr("Message box example!"));	label->setText(" About MessageBox ");	return;}void MessageBox::slotAboutQt(){	QMessageBox::aboutQt(this,"About Qt");	label->setText(" About Qt MessageBox ");	return;}void MessageBox::slotCustom(){	QMessageBox customMsgBox;	customMsgBox.setWindowTitle("Custom message box");	QPushButton *lockButton = customMsgBox.addButton(tr("Lock"),QMessageBox::ActionRole);	QPushButton *unlockButton = customMsgBox.addButton(tr("Unlock"),QMessageBox::ActionRole);	QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel);	customMsgBox.setIconPixmap(QPixmap(":/images/linuxredhat.png"));	customMsgBox.setText(tr("This is a custom message box"));	customMsgBox.exec();	if(customMsgBox.clickedButton() == lockButton)		label->setText(" Custom MessageBox / Lock ");	if(customMsgBox.clickedButton() == unlockButton)		label->setText(" Custom MessageBox / Unlock ");	if(customMsgBox.clickedButton() == cancelButton)		label->setText(" Custom MessageBox / Cancel ");	return;}

main.cpp

#include "messagebox.h"#include 
int main(int argc, char *argv[]){ QApplication a(argc, argv); MessageBox *w=new MessageBox; w->show(); return a.exec();}

效果图:

==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/04/10/2439999.html,如需转载请自行联系原作者
你可能感兴趣的文章
win32控制台应用程序中使用CString类型的方法
查看>>
关于authlib集成windows ad失败的分析并解决[草稿]
查看>>
centos5.4 x86_64禁用的服务
查看>>
python中单元测试的常用语句
查看>>
阿里Java面试题剖析:为什么使用消息队列?消息队列有什么优点和缺点?
查看>>
3.2.4 Shell脚本--函数的用法
查看>>
ssh-keygen -t rsa -f cloud.key ssh -i cloud.key <username>@<instance_ip>
查看>>
培训机构管理系统帮助机构解决管理问题
查看>>
我的友情链接
查看>>
C# 创建邮件合并模板并合并文本、图片
查看>>
DISCUZ官方论坛模仿开发日志(二)
查看>>
python基础(八)——多线程
查看>>
Java设计模式系列之策略模式
查看>>
12个国外优秀.Net开源项目(转)
查看>>
Expression Blend 4 激活
查看>>
将java项目转换成Web项目
查看>>
mysql 原理 ~ LRU 算法与buffer_pool
查看>>
个人经验~ 利用5.7的sys库更好的排查问题
查看>>
(转) ACM必备(学完一个就加亮一个)不多,就这些!
查看>>
数字图像处理中所用数学工具4---集合、逻辑操作与模糊集合
查看>>