Failed to get highlighting with TextEdit

TL; DR: TextEdit paint only selects text when I click on it. Nothing helps

I have a ListView with a QAbstractListModel model with string properties. These string properties are QSyntaxHighlighter and QSyntaxHighlighter used to display spell errors. I am creating a QSyntaxHighlighter descendant in Component.onCompleted from TextEdit . I have double checked the selection performed with the correct spell errors, and setFormat() from Highlighter is executed with the correct positions. The problem is that it draws the text in red (invalid) only when I click on TextEdit .

TextEdit lives in Flickable (to track the cursor) and Flickable lives in Rectangle (to have a nice background and border). Binding to some signals and calling update () TextEdit does not help.

After completing the rehighlight() I rehighlight() signal from the generated rehighlight() Syntax.

 Rectangle { id: descriptionRect height: 30 border.width: descriptionTextInput.activeFocus ? 1 : 0 clip: true Flickable { id: descriptionFlick contentWidth: descriptionTextInput.paintedWidth contentHeight: descriptionTextInput.paintedHeight anchors.fill: parent interactive: false flickableDirection: Flickable.HorizontalFlick height: 30 clip: true focus: false function ensureVisible(r) { if (contentX >= rx) contentX = rx; else if (contentX+width <= r.x+r.width) contentX = r.x+r.width-width; } TextEdit { id: descriptionTextInput width: descriptionFlick.width height: descriptionFlick.height text: description onTextChanged: model.editdescription = text Component.onCompleted: { globalModel.initDescriptionHighlighting(index, descriptionTextInput.textDocument) } onCursorRectangleChanged: descriptionFlick.ensureVisible(cursorRectangle) } } } 

Here is a small example project with a demonstration of how it does not work until you click on the text https://bitbucket.org/ribtoks/rehighlighdemo/src

Any ideas how I can solve this?

+8
qt syntax-highlighting qml
source share
1 answer

The problem is probably caused by QTBUG-44765 fixed in Qt 5.5.

Given the low error rate, I do not think that this can be practically circumvented.

You can get around this by adding an empty line to TextEdit when you finish syntax highlighting

 TextEdit { id: captionTextEdit width: wrapperFlick.width height: wrapperFlick.height text: display readOnly: true Component.onCompleted: { itemsModel.initHighlighter(index, captionTextEdit.textDocument) } Connections { target: itemsModel onUpdateTextEdit: { console.log("Update element at index: " + indexToUpdate) if (indexToUpdate == index) { console.log("Update me!") captionTextEdit.append("") } } } onCursorRectangleChanged: wrapperFlick.ensureVisible(cursorRectangle) } 

where updateTextEdit(indexToUpdate) is the new signal your Model element should emit.

itemsmodel.h

 signals: void updateTextEdit(int indexToUpdate); 

itemsmodel.cpp

 void ItemsModel::initHighlighter(int index, QQuickTextDocument *document) { // Signal mapper could be avoided if lamda slot are available (Qt5 and C++11) QSignalMapper* signalMapper = new QSignalMapper(this); if (0 <= index && index < m_ItemsList.length()) { SingleItem *item = m_ItemsList.at(index); SpellCheckHighlighter *highlighter = new SpellCheckHighlighter(document->textDocument(), item); QObject::connect(item, SIGNAL(spellCheckResultsReady()), highlighter, SLOT(rehighlight())); // TODO: Don't connect this slot for Qt 5.5+ to avoid performance overhead QObject::connect(item, SIGNAL(spellCheckResultsReady()), signalMapper, SLOT(map())); signalMapper->setMapping(item, index); } connect(signalMapper, SIGNAL(mapped(int)), this, SIGNAL(updateTextEdit(int))); } 

The full code is available here: https://bitbucket.org/swarta/rehighlighdemo/branch/workaround#diff

0
source share

All Articles