QWidget: Cannot create a QWidget without QApplication

(If you are searching for a solution to the above error then look at the last few paragraphs of this post, the first part is just me babbling about why I got my self into a situation where I had the above error).

So, I had an Qt5 application that used qml for the main interface, that worked fine.  I then wanted to extend the application with some content that was dynamically loaded from a website, this website contained some html5 with embedded JavaScript that needed to communicate with the Qt5 application, and the application needed to call into the JavaScript.

It should be straight forward, I first made the Android version, using the Android Webkit browser, it worked out of the box, binding into the Java world with JNI, and then into the WebKit browser, but when it came to using the WebKit that came with Qt, ohh, my.. that was when the trouble began…. of cause it was my lack of knowlegde and not the framework that were the issue.

First I tried with a qml WebView controller, that loaded the page fine, but when it came to the JS integration, I couldn’t figure it out, I then found that the QML WebView, and the QWebView is something completely unrelated (more or less)  where the QML WebView is very limited (unless you go to the private interface) the QWebView is much more reach and allows the integration with JS.

There are many examples on the web on how to use the QWebView class, they all went something like this:

QWebView *view = new QWebView(parent);
view->load(QUrl("http://www.google.com"));
view->show();

Now, what should “parent” be??? – I had no clue, I tried many different things, including leaving it blank, but each time the code crashed when executed…  I finally left it blank, but the code was still crashing with the message that is the title of this post.

I then tried to dig into what a QApplication was, and presto… – the issue was not related to the way I launched the webpage, it was in how I made the qml app run.  I originally had code like:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QmlInterface qmlInterface(&app);

    engine.rootContext()->setContextProperty(
                   "cInter", 
                   &qmlInterface);
    engine.load(
      QUrl(
        QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

I then found from the Qt docs, that a QApplication is a QGuiApplication, so replacing QGuiApplication with QApplication removed the run-time error and launched the browser:

int main(int argc, char *argv[])
{
    // QGuiApplication app(argc, argv);
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QmlInterface qmlInterface(&app);

    engine.rootContext()->setContextProperty(
                   "cInter", 
                   &qmlInterface);
    engine.load(
      QUrl(
        QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen ÷ = three