qt4 messagebox

refer to qt  QMessageBox Class Reference
头文件是:
 #include <QMessageBox>
1.
QMessageBox msgBox;
 msgBox.setText("The document has been modified.");
 msgBox.exec();
2.
 QMessageBox msgBox;
 msgBox.setText("The document has been modified.");
 msgBox.setInformativeText("Do you want to save your changes?");
 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
 msgBox.setDefaultButton(QMessageBox::Save);
 int ret = msgBox.exec();
 switch (ret) {
   case QMessageBox::Save:
       // Save was clicked
       break;
   case QMessageBox::Discard:
       // Don't Save was clicked
       break;
   case QMessageBox::Cancel:
       // Cancel was clicked
       break;
   default:
       // should never be reached
       break;
 }
3.
 int ret = QMessageBox::warning(this, tr("My Application"),tr("The document has been modified.\n" "Do you want to save your changes?"),
                                                         QMessageBox::Save | QMessageBox::Discard| QMessageBox::Cancel, QMessageBox::Save);
             

    QMessageBox::warning(this,"title","hello every body");
    QMessageBox::warning(this,"title",tr("hello every body"));

    QString name="song";
    int i=666;
    QMessageBox::warning(this,"title",tr("hello %1 ,%2").arg(name).arg(i));//参数只需指定%1 %2,不必%s %d
tr是QObject的一个静态成员函数,返回QString
QString QObject::tr ( const char * sourceText, const char * disambiguation = 0, int n = -1 )   [static]
Returns a translated version of sourceText, optionally based on a disambiguation string and value of n for strings containing plurals; otherwise returns sourceText itself if no appropriate translated string is available.





### QTMessageBox错误弹框的使用 在QT中,可使用`QMessageBox::critical`创建错误弹框。例如在引用[2]里,定义了一个在子线程中显示消息框的类`msgBox`,当`type`为2时,会调用`QMessageBox::critical`显示错误弹框: ```cpp void msgBox::onShow() { switch (type) { case 1: QMessageBox::information(NULL,title,msg,QString::fromLocal8Bit("ok")); break; case 2: QMessageBox::critical(NULL,title,msg,QString::fromLocal8Bit("ok")); break; case 3: QMessageBox::question(NULL,title,msg,QString::fromLocal8Bit("ok")); break; } this->deleteLater(); } ``` 上述代码中,当`type`为2时,会弹出一个错误弹框,标题为`title`,内容为`msg`,确认按钮显示“ok”。 ### 出现问题的解决办法 多线程调用时,在子线程里使用`QMessageBox`会出现程序卡死、无响应的问题。引用[3]中提到,可通过发送信号的方式解决该问题。示例代码如下: ```cpp // 头文件定义 // 信号 signals: void emitQMessageBox(QString _title,QString _text); // 槽函数 void SlotQMessageBox(QString _title, QString _text); // 源文件实现 // 初始化连接信号槽 connect(this, &类名::emitQMessageBox, this, &类名::SlotQMessageBox); // 需要用到的地方(另一个线程里) emitQMessageBox("错误", "打开失败! "); // 槽函数实现 void 类名::SlotQMessageBox(QString _title, QString _text) { QMessageBox::information(this, _title, _text); } ``` 上述代码中,在子线程里通过`emitQMessageBox`信号将消息传递给主线程,在主线程的槽函数`SlotQMessageBox`中显示消息框,避免了在子线程中直接使用`QMessageBox`导致的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值