如何在 Qt QMessageBox 中添加变量值?

How to add variable value in Qt QMessageBox?

printf("This machine calculated all prime numbers under %d %d times in %d 
  seconds\n", MAX_PRIME, NUM_OF_CORES, run_time);

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

我已经阅读了 QMessageBox 文档,但没有找到任何有用的信息。

QMessageBox 没有任何用处,因为它是 none 的业务 - 它只是在您传递字符串时显示它们。但是,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

首先你必须填写QString给你QMessageBox。您可以使用 QMessageBox 的 QString. Then you can show message box with static method information 的方法 arg 来完成。在您的情况下,代码将是:

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