QT中各种MessageBox的使用

原文地址为: 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


转载请注明本文地址: QT中各种MessageBox的使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值