Qt World Summit: Register Today!

QPlainTextEdit problems getting the right way to color items

  • I have a QPlainTextEdit widget which I'm using to display text with some color theme. The theme can change during execution, and unfortunately, the docs state clearly:

    'The default style sheet is applied to all newly HTML formatted text that is inserted into the document (...)'
    'Note: Changing the default style sheet does not have any effect to the existing content of the document.'

    So I'm in trouble when I apply a new theme because previous contents never display in a uniform manner. Changing the background color via CSS (using the background-color style property) will work, but it will apply the color only where text exists, and it won't complete until the "visual" end of the paragraph; it also won't change the background color vertically to the end of the widget. See screenshot 1 at this link:
    screenshot 1

    If I change the Base and/or Window QPalette color (without background-color in CSS), the background will change fin for the last paragraph and vertically until the end. However, it will also change the background of the previous lines without changing the text color though (see note 1), and this not only is ugly, but it will also make it very difficult to read text when text and background color match closely.
    screenshot 2

    The same ugly effect is created when I use HTML formatted text with <font color ...> tag in front of the text.

    Needles to say that I have tried a mix of QPalette and CSS changes, but they simply don't get along very well, the results were similar to the previous ones.

    The solution I would like to achieve is at least as the one shown next:
    screenshot 3

    Can somebody please help me? Did I miss any CSS style formatting, or maybe some QPalette colors?
    Also, I was looking at QSyntaxHighlighter, but it doesn't look to me like I could ever use it. The text printed is most of the times made of simple messages that have no regular syntax. Do you have any suggestions about it?

    Forgot to say that all text is printed in the widget using QPlainTextEdit::appendHtml() and can contain text words with a different color. It is achieved with an HTML string like this:

    <div class="some_class">some starting text:</div> <div class="some_other_class"> some message to show</div>

    where each class has its own text formatting CSS properties (set using QPlainTextEdit::setDefaultStyleSheet() )

  • Sorry to bump...
    I understand it might be hard to achieve my goal... so does anyone have any suggestion for a similar effect done in some other way than CSS, or even by using some other widget to display the messages?

  • Moderators

    This looks more like a list than a formatted block of text. I would suggest either employing a QListWidget and setting the foreground and background roles of the items or a QListView and a custom model that would define these roles and react to outside style changes.

Log in to reply