[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
Fri Apr 1 16:38:08 CEST 2022


Thanks to all for your comments!

I took a closer look given what Shawn wrote and looked at it through a debugger.

// main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>

int main( int argc, char* argv[] )
{
    QApplication app{ argc, argv };
    QQmlApplicationEngine engine;
    const QUrl url{ u"qrc:/xxxx/main.qml"_qs };
    QObject::connect( 
        &engine, 
        &QQmlApplicationEngine::objectCreated,
        &app, 
        [url]( QObject* obj, const QUrl& objUrl ) { if (!obj && url == objUrl){ QCoreApplication::exit(-1); } },
        Qt::QueuedConnection
    );
    engine.load( url );
    int exitCode{ app.exec() };
    return exitCode;
}

The QTextBrowser and QTextDocument instances are instantiated in the main thread but the QSGRenderThread does not start running until after the call to app.exec(), at which time the content supplied to the QTextBrowser (and to the QTextDocument) is rendered. At that point, the app crashes while the layout is occurring on the QSGRenderThread.

As the problem is occurring in the Qt code, of course being used in a way it is not intended to be used in (a widget being used in a QML app), I am not going to pursue this approach anymore.

This will need to run on iOS, so I don’t think creating a widget app would work well.

Using TextEdit is a possibility as mentioned and I will look into it. It has, however, another larger shortcoming in that I cannot alter its QTextDocument from C++ (as I understand I could have with QTextBrowser), which would be of great use to me beyond what I have described. I’ll have to think about how to make it work for my purposes.

I’ll consider logging a feature request (thanks for the suggestion!) once I have thought this through.

Thanks again!

Sincerely,

Jeffrey Brendecke

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220401/517a93b9/attachment.htm>


More information about the Interest mailing list