Unsolved QTextCharFormat from QTextCursor doesn't always report accurate format.
-
I was rewriting an older application of mine from C# to C++ with Qt. This program is meant to be a basic rich text editor with some extras for my daily use.
However, I'm getting incorrect results from the reported character format from highlighting text in a QTextEdit.
I have the following code on the
selectionChanged()
signal from a QTextEdit that updates the UI with the format of the currenttextCursor()
.QTextCursor cursor = ui.textEdit->textCursor(); auto format = cursor.charFormat(); ui.boldButton->setChecked(format.fontWeight() == QFont::Weight::Bold); ui.underlineButton->setChecked(format.underlineStyle() == QTextCharFormat::UnderlineStyle::SingleUnderline); ui.italicButton->setChecked(format.fontItalic()); _fontSizeModel->setValue(static_cast<int>(format.fontPointSize())); // a custom class ui.fontNameBox->setCurrentFont(format.font());
Moving the cursor around a large document without highlighting anything with various font size, bold/italic/underline anything updates the UI elements with the current format just fine. This problem is only with blocks of highlighted text.
The bottom of the doc currently has two newlines. I go to the bottom of the doc, add some text to that very bottom line, change the bold/italic/underline state and font size. After that, select the bottom line of text from left to right, the format still updates correctly. Then, instead select the text right to left up to the very beginning of the line, it now instead reports the format of the text above it.
In the image below, the "sdfsdfsdf" is the text I added. The highlight shows what I'm talking about in the above paragraph, and the top controls are what the above code is reporting.
This is not right and I don't know why it would do this no matter how I'm editing the text. If it helps, but I don't think it should make a difference, the test file I have was converted to html from RTF. If I add a new line instead of simply typing on that final blank line it's fine. Again, I don't think it should matter how I created the text or what broken state the underlying formatting is in because that's not what a user is going to care about.
The problem isn't just this, however, as, of course, the behavior of QTextCursor::mergeCharFormat will follow.
This is my bold button:
QTextCursor cursor = ui.textEdit->textCursor(); QTextCharFormat format = cursor.charFormat(); format.setFontItalic(!format.fontItalic()); cursor.mergeCharFormat(format);
Using the same highlight as in the picture above, clicking the B will cause the font size of the entire block to reset, since that's, of course, what it's reporting at the moment.
I am out of ideas and may just dump this application and return to the .NET version.
<rant>I wanted to use Qt because the text editor made some additional features easier to implement. I've written a few other much larger applications while learning Qt and have not come across something quite this intensely frustrating. Thankfully, those will never involve rich text editing. Hopefully someone can help.</rant>
-
Hi and welcome to devnet,
What version of Qt are you using ?
Are you experiencing the same with the Text Editor exemple ? -
I have not tried that project yet. I'm using 5.14.2.
[Edit]
I edited some of my code to match that project. In particular, I'm using this code to apply all font changes:
void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format) { QTextCursor cursor = textEdit->textCursor(); if (!cursor.hasSelection()) cursor.select(QTextCursor::WordUnderCursor); cursor.mergeCharFormat(format); textEdit->mergeCurrentCharFormat(format); }
Along with much of the same code to set font size, bold/italic/underline. Same results as before.
Previously, my code was copying the format of the current cursor. It now creates a blank QTextCharFormat, applies the one change, and merges.
It makes me think this is a bug in Qt because double clicking that block of text at the bottom also makes it report the correct font. Two ways correct. One way wrong. It even fails if I let the cursor select the entire word under the cursor. I'm installing the latest version. I also cannot get the font family to change, even using the exact code as in that demo.
[Edit 2]
Same result under Qt 5.15.
-
It indeed looks like you have found something.
You should check the bug report system to see if there's something related. If not, please consider opening a new report providing a minimal compilable example and the steps to reproduce it.
-
Was able to duplicate the same incorrect behavior with a simple example, so thankfully it means this is not dependent on any source text I had been using. I will be submitting a bug for this. Select the big 22 point text right to left and it reports it as 8 point.
[Edit]
-
Please provide a minimal compilable example on the bug report. This will help developer go way faster to identify and fix the issue.