How to show broken text from QTextDocument in QML?

I currently have a C ++ class inheriting from QQuickPaintedItem . I use it to draw mock-up, paginated rich text with QTextDocument via QTextDocument::drawContents (or by directly calling the QTextDocumenLayout draw method).

However, as stated in the QQuickPaintedItems documentation, there are thread issues that you need to know:

Warning When creating QObjects, emitting signals, triggering timers, and similar functions inside this function, special care must be taken, since they will have an affinity for the rendering stream.

In particular, in this case, QTextDocumentLayoutPrivate has timers that start / stop when QTextDocumenLayout::draw called. Unfortunately, QTextDocument and, therefore, timers live in the main qml stream, and paint is called in the rendering stream, which leads to messages like

QBasicTimer :: start: Timers cannot be started from another thread

Although this does not affect the functionality of my application (for now), it is probably not very good.

So my question is is there a better way to show broken text in QML (not necessarily including QQuickPaintedItem ).

+5
source share
1 answer

Currently, I'm still using QQuickPaintedItem , and when paint is called, I do the following:

  • First check to see if the QTextDocument value QTextDocument its affinity for the current thread. If so, I will continue as usual.

  • Otherwise, QMetaObject::invokeMethod used to call a method that moves the document into the rendering stream, and calls update to cause the redraw, which now works as the stream is bound correctly. At the end of paint affinity of the QTextDocument stream QTextDocument returned to the original stream.

It works, as far as I can tell (as, by the way, no more warnings), but it feels conceptually wrong.

+1
source

Source: https://habr.com/ru/post/1275831/


All Articles