QT中各种MessageBox的使用

简介:

MessageBox.h

#ifndef MESSAGEBOX_H
#define MESSAGEBOX_H

#include <QtGui>
#include "ui_messagebox.h"

class MessageBox : public QDialog
{
	Q_OBJECT

public:
	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 <QtGui/QApplication>

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

效果图:

image

imageimageimageimageimageimage

image


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/04/10/2439999.html,如需转载请自行联系原作者
相关文章
|
3月前
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
40 0
|
4月前
|
Python
tkinter之messagebox
tkinter之messagebox
18 2
|
11月前
|
开发者
Qt之标准对话框(QMessageBox、QFileDialog)
Qt之标准对话框(QMessageBox、QFileDialog)
117 0
|
11月前
|
数据安全/隐私保护
Qt之对话框(QDialog)
Qt之对话框(QDialog)
161 0
|
Linux C++ iOS开发
Qt 5——对话框QDialog
Qt 5——对话框QDialog
512 0
|
Python
Tkinter的Menu与Messagebox
菜单控件Menu与对话框控件Messagebox
72 0
Tkinter的Menu与Messagebox
|
编译器
在类库中使用MessageBox
机房收费系统(VB.Net版)过程中,在D层的SqlHelper类中有一个异常捕获处理,捕获了异常之后想使用MessageBox.Show()弹出异常描述,但在编译时出现了“未声明“MessageBox”。它可能因其保护级别而不可访问。”的错误:
在类库中使用MessageBox