简体   繁体   English

如何在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);

I want this output to be printed in QMessageBox text Box. 我希望将此输出打印在QMessageBox文本框中。

I have gone through QMessageBox documentation didn't find anything helpful. 我遍历了QMessageBox文档,没有发现任何有用的信息。

QMessageBox has nothing for it because it's none of its business - it just displays strings as you passed them. QMessageBox没什么用,因为它与它无关,它只是在您传递字符串时显示字符串。 However, QString does provide methods for formatting data replacing placeholders using the arg method: 但是, 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#argument-formats

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

First of all you must fill QString for you QMessageBox . 首先,您必须为QMessageBox填充QString You can do it with method arg of QString . 您可以使用QString的 arg方法来实现。 Then you can show message box with static method information of QMessageBox. 然后,您可以显示带有QMessageBox静态方法信息的消息框。 In your case code will be: 在您的情况下,代码将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM