4

I've been using a QMessageBox to display the outcome of a statistical test. It's nice, because I can put a summary result in the informative text, then the full result in the detailed text. The trouble is, the full result is a table, so I'd like it to be monospaced so that it looks right, and QMessageBox doesn't use a monospace font in the detailed text area.

So I'm looking at either subclassing QMessageBox, or subclassing QDialog to make something that looks like a QMessageBox but uses a monospace font in the detailed text area. I'm a bit rusty at the moment, and having a hard time figuring out which is the better option. Could I subclass QMessageBox, just add my own QTextEdit and my own "show detailed text" button, and leave the QMessageBox detailed text area and button hidden? Or is there some easier way to do this?

demonplus
  • 5,613
  • 12
  • 49
  • 68
rainbowgoblin
  • 1,221
  • 12
  • 28

4 Answers4

4

You can use html text in the fields of a QMessageBox, that would be the easiest way. As a hint, try putting

 <pre>Test</pre>

in your QString.

Any other customization of the message box will probably imply a subclass, though.

Vince
  • 3,497
  • 2
  • 19
  • 15
  • 2
    HTML seems to work for the informative text, but not the detailed text. It seems to use ASCII (I can use special characters, like nonbreaking spaces, but have to use escaped ASCII codes for them as opposed to HTML codes). Tags like
     appear literally in the detailed text area.
    – rainbowgoblin Mar 20 '14 at 06:15
4

I did not find better than this:

setStyleSheet("QTextEdit { font-family: monospace; }");

It's a bit hacky because (1) it uses stylesheets, which may conflicts with your way of styling your widget and (2) it relies on the fact that the detailed text is in a QTextEdit and is the only such element, which is not officially ensured by the API. But it works. :D

Lithy
  • 817
  • 12
  • 23
2

Here is a working example, based on the answer of Lithy:

import sys
from PySide2.QtCore import *
from PySide2.QtWidgets import *

table_text = '\
Name    Flower\n\
------  -------\n\
Violet  Yes\n\
Robert  No\n\
Daisy   Yes\n\
Anna    No\n\
'

class Widget(QWidget):
    def __init__(self, parent= None):
        super(Widget, self).__init__()
        warning_text = 'warning_text'
        info_text = 'info_text'
        pt = 'colour  name'
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Warning)
        msg.setText(warning_text)
        msg.setInformativeText(info_text)
        msg.setDetailedText("{}".format(table_text))
        msg.setTextInteractionFlags(Qt.TextSelectableByMouse)
        # print all children and their children to find out which widget
        # is the one that contains the detailed text
        for child in msg.children():
            print('child:{}'.format(child))
            print(' {}'.format(child.children()))
        pname = 'QMessageBox'
        cname = 'QTextEdit'
        msg.setStyleSheet(
            """{} {} {{ background-color: red; color: black; font-family: Courier; }}""".format(pname, cname))
        msg.exec_()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    Widget()
NZD
  • 1,780
  • 2
  • 20
  • 29
0

You can use html text in the detailedText of a QMessageBox using this hack:

QString html_formatted_text;
QMessageBox mb;
mb.setDetailedText(html_formatted_text);
// Find detailed text widget
auto te = mb.findChild<QTextEdit*>();
if (te) {
    te->setHtml(mb.detailedText());
}
mb.exec();