Important: Please read the Qt Code of Conduct -

[SOLVED] QML Application Crashes on window->show

  • Hello,

    I made a very basic application where I am just creating a rectangle in QML and showing it from my main as follows:

    @#include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQuickWindow>

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

    QQmlApplicationEngine engine;
    QObject *topLevel = engine.rootObjects().value(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    return app.exec&#40;&#41;;


    @import QtQuick 2.0

    Rectangle {
    width: 100
    height: 62

    For some reason when I run the above it says that "The program unexpectedly finished" and that the .exe has crashed. It seems that what I am doing is very basic so I do not see what can cause it to crash.

    If I comment out the window->show portion it does not crash until I tell the program to stop running. Any ideas?

  • Lifetime Qt Champion


    Two possibilities:

    topLevel is NULL

    topLevel is not a QQuickWindow thus window is NULL

  • [quote author="SGaist" date="1393534511"]Hi,

    Two possibilities:

    topLevel is NULL

    topLevel is not a QQuickWindow thus window is NULL[/quote]

    Thanks for the quick response. In the back of my mind I knew that was the problem. I guess the better question is why is that happening. I have used this same code numerous times without error, but for some reason now I am having a problem. I checked my path to main.qml and it is fine. I am not sure how else to debug it.

  • I solved the problem. It was that in my main.qml, my top level component was a rectangle. I changed it to ApplicationWindow so that I actually had a window and it worked. I had to add import QtQuick.Controls 1.0 as well.

    Thanks for the help SGaist!

Log in to reply