1

When using myQTextEdit.append() the style of the inserted text is as follows (Qt 5.14 documentation):

"The new paragraph appended will have the same character format and block format as the current paragraph, determined by the position of the cursor."

However I would find it convenient to be able to append text with a neutral style.

What causes my problem is this: I have a log window in the form of a QTextEdit where I append text (mostly neutral but some element may be coloured, etc.). Since it's for log purposes, the QTextEdit is read-only and the text elements are always added at the end (append()). This is fine as long as the user never clicks on the text. When clicking on a part of the QTextEdit, the cursor position is changed. It's not an issue for the position since I use append() which inserts text at the end, even if the cursor is somewhere else. However, if the user clicked on something with a non-neutral style, the afterwards appended text will have this style as well, which is not desirable.

What would be interesting for me would be to either block the cursor so that the user can't tamper with the styles or to append without basing the style on the current paragraph.

Is there a way to change this behaviour, other than by subclassing QTextEdit?

As mentioned, I could check the cursor position before doing any append() (and set the cursor at the end of the document if it has been moved) but if it exists, I would prefer a more "global" solution.

DylanM
  • 333
  • 2
  • 5
  • 17
  • Maybe, it's possible with a bit more effort: [QTextCursor::insertHtml()](https://doc.qt.io/qt-5/qtextcursor.html#insertHtml) where the `QTextCursor` is set to end of document before. (I believe this could be a copy of the [QTextEdit::textCursor()](https://doc.qt.io/qt-5/qtextedit.html#textCursor) to keep the visual cursor at its original position.) – Scheff's Cat Jun 17 '20 at 09:35
  • Yes true, I could set the cursor to the end of document before each text insertion to avoid unwanted style effects. What surprises me is that there is no way to "block" user/click cursor modification (even on a read-only QTextEdit). – DylanM Jun 17 '20 at 09:41
  • Can you provide a [mcve]? I'm not sure whether I fully got your intention. – Scheff's Cat Jun 17 '20 at 09:42
  • I edited the text with more details which I think makes the intention clearer. If not, I'll try to provide a minimal reproducible example (but the issue is mostly caused by user/human interaction with graphical elements which is delicate to illustrate in a code sample). – DylanM Jun 17 '20 at 10:18
  • Great job at creating a very clear MCVE, it turns out to reflect the issue way more clearly that I thought was possible. Something to keep in mind on my side! – DylanM Jun 19 '20 at 09:41

1 Answers1

1

I tried to reproduce in an MCVE what OP described:

// Qt header:
#include <QtWidgets>

// main application
int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup GUI
  QTextEdit qTextEdit;
  qTextEdit.show();
  // populate text editor
  qTextEdit.append(QString::fromUtf8(
    "<p>This is some text...</p>"
    "<p style='color: red'>...followed by more red text...</p>"
    "<p style='color: blue; font-weight: bold'>...followed by more fat blue text.</p>"));
  // test QTextEdit::append() like described by OP:
  qTextEdit.setTextCursor(QTextCursor(qTextEdit.document()->findBlockByNumber(1)));
  qTextEdit.append("TEST. (Reproduce what OP described.)");
  qTextEdit.append("<p>TEST. (A possible fix.)</p>");
  // runtime loop
  return app.exec();
}

Output:

snapshot of MCVE

So, a possible fix is to provide the text to append with mark-up.

If it's just raw text the simplest solution is to wrap it in "<p>" and "</p>".

Btw. if it's just raw text I would recommend some additional adjustments to make it proper HTML according to the Supported HTML Subset. Namely, I would search and replace the usual XML meta characters like I did it e.g. in my answer to SO: qt plaintextedit change message color.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56