如何在qt qmessagebox中添加可变值


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

最新更新