21

I have an action which create QMessageBox. In that dialog I want to print a list, which contains several items. I have the following code:

void MainWindow::onAboutActivated(){
qDebug() << "about";
QMessageBox::about(this,
                   "Autor: \n"
                   "\n"
                   "Umoznuje:"
                   "<ul>"
                   "<li> Item 1 </li>"
                   "<li> Item 2 </li>"
                   "<li> Item 3 </li>"
                   "</ul>");

However this does not print the list, but text with html tags. How can I print the list? Any ideas?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Jan
  • 1,054
  • 13
  • 36

2 Answers2

36

Don't mix newlines \n with html-tags. Change the newlines to <br> and then the text format is automatically recognized.

3

It seems you are setting the dialog title instead of dialog contents. This works for me:

void MainWindow::onAboutActivated(){
qDebug() << "about";
QMessageBox::about(this, "Dialog Title",
                   "Autor: \n"
                   "\n"
                   "Umoznuje:"
                   "<ul>"
                   "<li> Item 1 </li>"
                   "<li> Item 2 </li>"
                   "<li> Item 3 </li>"
                   "</ul>");

The default text format for QMessageBox is Qt::AutoText which should detect html tags inside your string, so you should be able to continue using the about static method without the need to instantiate a QMessageBox object.

Masci
  • 5,864
  • 1
  • 26
  • 21
  • I think it was just a typo that the OP forgot to include the dialog title. The code that the OP posted (without the title) wouldn't compile but the OP says that it prints the HTML tags and doesn't mention any problems compiling. Your code compiles fine, but doesn't solve the actual problem which is that the dialog box prints the HTML tags just like that instead of interpreting the HTML as a list. The other answer solves that problem. – Donald Duck Apr 09 '17 at 17:35