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();
}

效果图:

时间: 2024-08-06 00:53:04

QT中各种MessageBox的使用的相关文章

[Qt教程] 第29篇 XML(三)Qt中的SAX

[Qt教程] 第29篇 XML(三)Qt中的SAX 楼主  发表于 2013-5-21 22:27:59 | 查看: 615| 回复: 5 Qt中的SAX 版权声明 该文章原创于Qter开源社区(www.qter.org),作者yafeilinux,转载请注明出处! 导语 我们前面讲述了用DOM的方法对XML文档进行操作,DOM实现起来很灵活,但是这样也就使得编程变得复杂了些,而且我们前面也提到过,DOM需要预先把整个XML文档都读入内存,这样就使得它不适合处理较大的文件.下面我们讲述另一种读取

令你心动的Asp.net 开发中的MessageBox控件

asp.net|控件 令你心动的Asp.net 开发中的MessageBox控件 相信使用ASP.NET做Web开发的程序员们,都会怀念使用MessageBox.Show( "" )的信息提示方式吧.只可惜在ASPX中并不支持此类功能函数,至多也就是在脚本中使用alert( " " )来达到目的.要是想在服务器端的代码中来控制客户端信息提示窗体的显示,就不能直接进行控制了.以下就是我所设计开发的一个用于在服务器端来控制客户端信息提示窗体显示的Web自定义控件,很好用

Linux系统下QT中的多线程编程

Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功能.为了满足 用户构造复杂图形界面系统的需求,Qt 提供了丰富的多线程编程支持. Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功能.为了满足 用户构造复杂图形界面系统的需求,Qt 提供了丰富的多线程编程支持.从 2.2 版本开始,Qt 主要从下 面三个方面对多线程编程提供支持:一.构造了一些基本的与平台无关的线程类:二.提交用户自定义事 件的 Thread-saf

c++-Qt中,怎么链接写好的C++功能实现

问题描述 Qt中,怎么链接写好的C++功能实现 在Qt中,怎么链接写好的C++功能实现 在Qt中,怎么链接写好的C++功能实现 在Qt中,怎么链接写好的C++功能实现 解决方案 Qt Manual中有对Library加载的详细说明,静态库只要加头文件所在目录,在代码中包含需要的头文件,并设置静态库lib所在目录到库目录中: 如果是dll,则需要dll import/export来标识需要导入/导出的类,然后设置lib相关目录,则可以在代码中使用dll中的类和函数了. 解决方案二: 链接无非就是链

c++-qt中函数参数类型不同也能实现其功能?

问题描述 qt中函数参数类型不同也能实现其功能? 图中data为QByteArray类型,而函数原型为const char类型,broadcast为枚举类型,而函数原型为hostaddress类型.为什么不同也能够实现其功能呢? 解决方案 这里发生了隐式类型转换 QString raw_string = ""haasd123ha""; QByteArray byte_instrument = raw_string.toLocal8Bit(); const char

qt c++-Qt中,到底如何实现主窗口和子窗口之间的通信?

问题描述 Qt中,到底如何实现主窗口和子窗口之间的通信? RT,比如,当子窗口关闭时,重新打开主窗口.这个是如何通信的,希望能给上例子.我知道是用信号和槽,可是两个窗口(类)之间的信号和槽我还不太会.网上也没找到具体的例子.希望大婶们能给个好点的直观的例子. 解决方案 主窗体类为A,子窗体类为B,在A中实例化B,其对象为b,关闭b,但不要释放b,调用b的public方法返回数据.仔细理解. void A::buttonClick(){ B b; b.exec(); b.getData();} 或

qt中格子布局删除布局中的控件,行数未减少

问题描述 qt中格子布局删除布局中的控件,行数未减少 //原先这个格子布局中有4个按钮,现在删除第一个的按钮,在最后以后添加一个按钮. QLayoutItem* pDeleteItem(NULL); pDeleteItem = m_pGridLayout->itemAt(0); if(pDeleteItem != NULL) { QWidget* pWidget = pDeleteItem->widget(); m_pGridLayout->removeWidget(pWidget);

母窗口-QT中怎么在一个窗口中获得另一个窗口的指针?

问题描述 QT中怎么在一个窗口中获得另一个窗口的指针? 我用Widget作为母窗口,在其主函数中用信号槽建立了一个Dialog的窗口,那么这个Dialog窗口是不是Widget的子窗口呢?如果是的话,我想通过信号槽在Dialog中调用Widget中的数据,这样的话Widget母窗口应该是SIGNAL信号,在信号槽函数中需要使用它的指针.我在建立Dialog窗口的时候用new Dialog(this)将主函数指针传递给子函数,在子函数函数体的信号槽中槽函数写的parentWidget () 获得母

QT中QListWidget窗口中条目更新,如何让它动态显示出来

问题描述 QT中QListWidget窗口中条目更新,如何让它动态显示出来 我做的是一个公交车报站系统,在点击完模拟按钮后,会模拟走完整条路线,但是QListWidget不会动态更新,其中的数据处理部分还是会进行 附上模拟按钮的代码 /* simulate */ void MyDialog::on_pushButton_3_clicked() { while (1) { // 先清空显示 ui->listWidget->clear(); QFile file("bus_.xml&qu