qt 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

imageimageimageimageimage

转载于:https://www.cnblogs.com/wangjian8888/p/8676753.html

Qt的QMessageBox是一个用于显示各种类型的对话框的类。有几种方法可以自定义和使用QMessageBox。 首先,你可以使用定时关闭来提示框。可以创建一个QMessageBox对象,并使用QTimer::singleShot()函数在一定时间后关闭对话框。例如,可以使用以下代码创建一个消息框,并在1.5秒后自动关闭: QMessageBox *box = new QMessageBox(QMessageBox::Information,"Title","Content"); QTimer::singleShot(1500,box,SLOT(accept())); box->exec(); 另一种自定义QMessageBox的方法是创建一个继承自QDialog的类,并将MessageBox的基类修改为这个自定义类。这样可以通过在自定义类中添加额外的功能和样式来实现自定义MessageBox。你可以添加以下代码来添加自定义样式: #include <QDialog> class MessageBox : public QDialog { }; 此外,QMessageBox还提供了一些常用的静态函数来创建不同类型的消息框。例如,使用QMessageBox::warning()函数可以创建一个警告对话框,如以下代码所示: QMessageBox::warning(this,"Title","Error Message"); 你也可以使用QMessageBox::information()函数创建一个标准信息提示框,并设置按钮的样式和行为。例如,以下代码创建了一个带有标题和内容的信息提示框,并显示了Yes和No按钮: QMessageBox::information(NULL, "Title", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); 请根据你的具体需求选择合适的方法来使用Qt的QMessageBox类。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Qt QMessageBox使用详解](https://blog.csdn.net/qq_37529913/article/details/115455769)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [QT 写一个属于自己的消息弹窗MessageBox](https://blog.csdn.net/cpp_learner/article/details/124612475)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值