定义自己的QMessageBox

QMessageBox类是Qt程序中经常用到的消息提示框类简单的消息提示框可以这样定义:


QMessageBox::critical(NULL,tr("错误"),tr("错误原因"));

这是一个简单错误提示框,QMessageBox 封装了下面几个提示框,可以直接使用:

void	about ( QWidget * parent, const QString & title, const QString & text )
void	aboutQt ( QWidget * parent, const QString & title = QString() )
StandardButton	critical ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
StandardButton	information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
StandardButton	question ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
StandardButton	warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )


StandardButton 的意思是可以给,critical,information,question ,warning添加想要的类型的Button,这里就不多做介绍了。
言归正传,编写自己的QMessageBox的方法如下:

方法1:

	QMessageBox MyQMessageBox; //构造对象
	MyQMessageBox.setWindowTitle(tr("退出"));//设置标题
	MyQMessageBox.setIcon(QMessageBox::Question);//设置图标
	MyQMessageBox.setText(tr("是否要退出?"));//提示内容
	QPushButton *confirm = MyQMessageBox.addButton(tr("确定"),QMessageBox::ActionRole);//添加按钮
	QPushButton *cancel = MyQMessageBox.addButton(tr("取消"),QMessageBox::ActionRole);
	MyQMessageBox.exec();
	if(MyQMessageBox.clickedButton()== confirm)//当点击确定时
	{
		this->close();
	}
	else
	{
		e->ignore();
	}

方法2:

	QMessageBox msg;
	msg.setWindowTitle(tr("退出"));
	msg.setText(tr("是否要退出?"));
	msg.setIcon(QMessageBox::Information);
	msg.setButton(1,tr("确定"));
	msg.setButton(2,tr("取消"));
	if(msg.exec();)
	{
		this->close;
	}	
	else
		NULL;


上面两种方法其实很相似,只不过是QMessageBox的按钮的设置方式不同。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值