Category Archives: Qt5

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();
}

 

Do not use numbers or underscores as first characters in Android/Java package names

Well, I’ve been trying different things out at work the last week in order to make some small tests and evaluating different design ideas – I can’t reveal what it’s all about yet, except I could mention some buzz words that doesn’t really say anything (scalability, cross-X, open source, market killer, etc. etc. etc. some of these might be true, some might be wishes and some might just be pure imagination).

Anyway as part of this I have been writing a number of small Android applications – and guess what, I learned the hard way that you can’t use _ (Underscores) or numbers as the first character  like systems.mikek.oneforall is ok, but systems.mikek.1forall isn’t – which might make sense, as numbers are not allowed as the first character in identifiers in many languages (even they are fine for URL’s) – but I was a bit more surprised that prefixing the number with _ wasn’t allowed either – so no systems.mikek._1forall either…  lesson learned 🙂

^__^
Mike

btw. I don’t know if this is just for Android, or generic for Java (actually I only tried it in an app made in the Qt5 framework for Android).