QT中各种MessageBox的使用

本文介绍了一个使用Qt库创建的消息框示例程序。该程序通过不同的按钮触发多种类型的消息框,如提问、信息提示、警告、严重错误等,并展示如何自定义消息框。

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

 

<think> 嗯,用户正在询问Qt使用MessageBox的方法。用户提供了几段引用内容,包含了技术示例和代码片段,需要结合这些资料来整理一个全面的回答。 用户可能是个Qt开发者,正在实现对话框功能。从引用内容看,用户似乎更关注实际用法而非理论,特别是引用[2]和[4]中展示的具体调用方式很有参考价值。需要强调实用性和代码示例的清晰度。 梳理出三个核心用法:静态函数方式、实例化对象方式、自定义方式。静态函数最简洁,实例化对象更灵活,自定义则适合复杂场景。每个方法都要配上代码示例,比如引用[2]的information/warning调用和box.exec()就很典型。 注意到用户提供的引用[3]提到尺寸调整问题,虽然用户没直接问,但可以补充到高级用法里。另外引用[4]展示了错误处理的实际应用,可以用来丰富示例场景。 要避免直接复制引用中的代码片段,而是整合成更规范的示例。比如把warning示例和密码验证场景结合,这样更有实操价值。最后考虑生成相关问题时要覆盖用户可能延伸的需求,比如样式定制、阻塞特性等技术点。 </think> 以下是Qt中`QMessageBox`的详细使用方法总结,结合您提供的引用资料: ### 一、基础调用方式(静态函数) 1. **信息提示框** ```cpp QMessageBox::information(nullptr, "标题", "操作成功!"); ``` *适用于普通提示[^2]* 2. **警告框** ```cpp QMessageBox::warning(this, "警告", "文件保存失败"); ``` *常用于错误提示(如密码校验失败示例)[^4]* 3. **错误提示框** ```cpp QMessageBox::critical(parentWidget(), "严重错误", "内存分配失败"); ``` 4. **询问框** ```cpp int ret = QMessageBox::question(this, "确认", "确定删除?", QMessageBox::Yes | QMessageBox::No); if(ret == QMessageBox::Yes) { // 执行删除操作 } ``` ### 二、对象实例化方式(灵活控制) ```cpp QMessageBox msgBox; msgBox.setWindowTitle("自定义标题"); msgBox.setText("<b>详细内容</b>可包含HTML格式"); msgBox.setIcon(QMessageBox::Information); msgBox.addButton("确定", QMessageBox::AcceptRole); msgBox.addButton("取消", QMessageBox::RejectRole); if(msgBox.exec() == QMessageBox::AcceptRole) { // 点击确定后的操作 } ``` *可通过对象方法自定义按钮和内容[^2][^3]* ### 三、尺寸调整技巧 ```cpp QMessageBox msgBox; msgBox.setFixedSize(300, 200); // 固定尺寸 // 或 msgBox.setMinimumSize(400, 300); // 最小尺寸 ``` *解决默认尺寸过小的问题[^3]* ### 四、实际应用示例(密码验证场景) ```cpp void RegDialog::onSubmitClicked() { if(passwordEdit->text() != confirmEdit->text()) { QMessageBox::warning(this, "注册错误", "两次密码输入不一致!"); passwordEdit->clear(); confirmEdit->clear(); } else { // 注册逻辑 } } ``` *直接调用静态函数实现错误提示[^4]* --- ### 高级用法建议 1. **自定义按钮文本** ```cpp msgBox.addButton("强制保存", QMessageBox::ActionRole); ``` 2. **设置详细文本** ```cpp msgBox.setDetailedText("错误日志:..."); ``` 3. **修改图标** ```cpp msgBox.setIconPixmap(QPixmap(":/icons/custom.png")); ``` ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值