Search code examples
c++qtqt5qmessagebox

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.

I have gone through QMessageBox documentation didn't find anything helpful.


Solution

  • First of all you must fill QString for you QMessageBox. You can do it with method arg of QString. Then you can show message box with static method information of 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));