printf("This machine calculated all prime numbers under %d %d times in %d
secondsn", MAX_PRIME, NUM_OF_CORES, run_time);
我希望将此输出打印在QMessageBox
文本框中。
我已经通过QMessageBox
文档没有找到任何有用的东西。
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/qtstring.html#argument-formats
http://doc.qt.io/qt-5/qtring.html#arg
首先,您必须为您的QMessageBox
填充QString
。您可以使用QString的方法arg
进行操作。然后,您可以使用Qmessagebox的静态方法显示消息框。在您的案例中,代码将是:
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));