[Interest] Embedding a QTextBrowser in a QML application: QObject: Cannot create children for a parent that is in a different thread

Jeffrey Brendecke jeffrey.brendecke at gmail.com
Wed Mar 30 03:03:12 CEST 2022

For an app I am working on I need a component for displaying HTML rich text. TextEdit does not provide an important feature I need present in QTextEdit and QTextBrowser: scrollToAnchor( const QString& ).

The app will have to work on iOS, so WebEngine is out of the question, and I would not want to have all the overhead of having a web browser in the app anyways.

Given the text itself is fairly simple (no images, just formatted text), QTextBrowser gives me what I need.

I have been looking into trying to embed a QTextBrowser into a QML application using the approach in this project:

https://www.alex-spataru.com/blog/using-qt-widgets-in-your-qmlqtquick-applications <https://www.alex-spataru.com/blog/using-qt-widgets-in-your-qmlqtquick-applications>. (QmlPlainTextEdit)

A similar approach is used here:

https://github.com/mosolovsa/qmlplot <https://github.com/mosolovsa/qmlplot>

This involves:
* Creating a class deriving from QQuickPaintedItem
* Encapsulating an instance of QTextBrowser instantiated with a nullptr parent as a member in that class
* Rendering from the QTextBrowser into the QQuickPaintedItem derivative
* Hooking up the necessary events between the QQuickPaintedItem and the QTextBrowser instance

That all seems to be no problem. The problem I get results in a segmentation fault:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x5617e2ebed80), parent's thread is QThread(0x5617e2958860), current thread is QSGRenderThread(0x5617e2d18400)

The crash occurs after passing the URL to open via QTextBrowser::setSource( const QUrl&, QTextDocument::ResourceType ) though not immediately.

Note: I am using Qt 6.3.0-rc on Kubuntu Linux 21.10.

The qmlplot application will compile and run on Qt 5.15.8 (with warnings) but not on 6.3.0-rc (lots of errors and warnings). However, it does not involve a widget containing an instance of QTextDocument. I have not tried the code directly in QmlPlainTextEdit, but I did notice the source sites are no longer available.

I am aware that, assuming this can be made to work, that performance may be CPU intensive especially with antialiasing, but I wanted to give it a try before looking into alternatives.

Thanks very much in advance!


Jeffrey Brendecke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220330/98be0626/attachment.htm>

More information about the Interest mailing list