QT对话框之QMessageBox

  QT提供了五个标准对话框接口,分别是critical、information、warning、question、about。当然我们也可以自己定义对话框。下面将一一详细介绍使用方法。

    StandardButton 的意思是可以给,critical,information,question ,warning添加想要的类型的Button

 

Critical:

   QMessageBox::StandardButton test;
   test:QMessageBox::critical(this,tr("错误"),tr("错误原因"), QMessageBox::Yes | QMessageBox::No);
    if(test == QMessageBox::Yes)
    {
    }
    else
    {
    }

 

Information:

  QMessageBox::information(this, "Title", "Text");

 

Warning:

   QMessageBox::StandardButton test;
        test:QMessageBox::warning(this,tr("警告"),tr("密码错误!"),QMessageBox::Yes | QMessageBox::No);
        if(test == QMessageBox::Yes)
        {
        }
        else
        {
        }

 

Question:

 QMessageBox::StandardButton test;
        test = QMessageBox::question(this, "Title", "确定?", QMessageBox::Yes | QMessageBox::No);
        if(test == QMessageBox::Yes)
        {
        }
        else
        {
        }

 

About:about是没有双按钮的,并且其支持HTML标签。

    QMessageBox::about(this, "About", " <font color='blue'>信息</font>");

 

 

自定义QMessageBox因为这是个模态对话框,需要有它自己的事件循环,所以我们用exec(),而不是用show()。

  QMessageBox msg;
        msg.setWindowTitle(tr("Title"));
        msg.setText(tr("Text"));
        msg.setIcon(QMessageBox::Information);
       // msg.setIconPixmap(QPixmap("1.png"));
        msg.addButton(tr("NO"),QMessageBox::NoRole);
        msg.addButton(tr("YES"),QMessageBox::YesRole);
        if(msg.exec())
        {
           QMessageBox::about(this, "About", " <font color='blue'>信息</font>");
        }
        else
           this->close();

 

 

附上描述:

Constant                                        Value             Description
QMessageBox::InvalidRole              -1               The button is invalid.
QMessageBox::AcceptRole              0                Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox::RejectRole               1                Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox::DestructiveRole       2                 Clicking the button causes a destructive change (e.g. for Discarding Changes)
                                                                             and closes the dialog.
QMessageBox::ActionRole               3                Clicking the button causes changes to the elements within the dialog.
QMessageBox::HelpRole                  4                The button can be clicked to request help.
QMessageBox::YesRole                    5                 The button is a "Yes"-like button.
QMessageBox::NoRole                     6                 The button is a "No"-like button.
QMessageBox::ApplyRole                8                 The button applies current changes.
QMessageBox::ResetRole                7                  The button resets the dialog's fields to default values.

中文显示(编码):

QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));

 

### QT 对话框使用方法 #### 1. Qt 标准对话框概述 Qt 提供了一组预定义的标准对话框,这些对话框都继承自 `QDialog` 类。它们可以用来实现常见的用户交互需求,例如显示消息、打开文件、选择颜色等功能[^1]。 #### 2. 常见的 Qt 对话框及其用法 ##### 2.1 `QMessageBox` `QMessageBox` 是一个常用的对话框类,用于向用户提供信息或请求确认操作。它可以通过静态函数轻松调用,而无需实例化对象。 以下是展示关于对话框的一个简单示例: ```cpp #include <QApplication> #include <QMessageBox> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 调用静态函数 about() 来显示关于对话框 QMessageBox::about(nullptr, "About", "This is a simple application using QMessageBox."); return app.exec(); } ``` 上述代码展示了如何通过 `QMessageBox::about()` 方法来创建并显示一个简单的关于对话框[^3]。 ##### 2.2 `QInputDialog` `QInputDialog` 可以让用户输入一些基本的数据类型,如字符串、整数、浮点数等。下面是一个获取用户输入字符串的例子: ```cpp #include <QApplication> #include <QInputDialog> #include <QDebug> int main(int argc, char *argv[]) { QApplication app(argc, argv); bool ok; QString text = QInputDialog::getText(nullptr, "Input Dialog", "Enter your name:", QLineEdit::Normal, "", &ok); if (ok && !text.isEmpty()) { qDebug() << "Your name is:" << text; } return app.exec(); } ``` 这段代码演示了如何利用 `QInputDialog::getText()` 获取用户的姓名输入[^4]。 #### 3. 总结 以上两个例子分别介绍了 `QMessageBox` 和 `QInputDialog` 的基础用法。实际开发中可以根据具体需求选用不同的对话框类型,并结合其他组件完成更复杂的交互逻辑。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的书包哪里去了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值