【问题标题】:How to add variable value in Qt QMessageBox?如何在 Qt QMessageBox 中添加变量值?
【发布时间】:2016-06-23 05:47:32
【问题描述】:
printf("This machine calculated all prime numbers under %d %d times in %d 
  seconds\n", MAX_PRIME, NUM_OF_CORES, run_time);

我希望将此输出打印在QMessageBox 文本框中。

我浏览了QMessageBox 文档,没有发现任何有用的信息。

【问题讨论】:

标签: c++ qt qt5 qmessagebox


【解决方案1】:

QMessageBox 没有任何作用,因为它与它无关——它只是在您传递字符串时显示字符串。但是,QString 确实提供了使用arg 方法格式化数据替换占位符的方法:

QMessageBox::information(parent,
     QString("This machine calculated all prime numbers under %1 %2 times in %3 seconds")
         .arg(MAX_PRIME)
         .arg(NUM_OF_CORES)
         .arg(run_time), "Message title");

http://doc.qt.io/qt-5/qstring.html#argument-formats

http://doc.qt.io/qt-5/qstring.html#arg

【讨论】:

    【解决方案2】:

    首先你必须为你填写QStringQMessageBox。您可以使用QString 的方法arg 来实现。然后你可以用QMessageBox的静态方法information显示消息框。在您的情况下,代码将是:

    QMessageBox::information(nullptr/*or parent*/, "Title",  
        QString("This machine calculated all prime numbers under %1 %2 times in %3 seconds")
        .arg(MAX_PRIME).arg(NUM_OF_CORES).arg(run_time));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-30
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 2013-11-23
      相关资源
      最近更新 更多