c++qtqmlc++17qqmlapplicationengine

QML window is not visible when loaded from QQmlApplicationEngine


Problem

The problem is that I have some C++ code that loads a QML file called gui.qml, it compiles with no problem, but when running it doesn't display the QML app window but also doesn't give any errors.

Code

Here is the C++ code that I use to load the qml (edit: used qrc to load qml):

#include <QGuiApplication>
#include <QQmlApplicationEngine>

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

    const QUrl url("qrc:/components/gui.qml");

    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app,
                     [url](QObject *obj, const QUrl &objUrl) {
                         if (!obj && url == objUrl)
                             QCoreApplication::exit(-1);
                     },
                     Qt::QueuedConnection);
    
    engine.load(url);

    return app.exec();
}

Here is the QML code that I want to display:

import QtQuick 6.4
import QtQuick.Controls 6.4
import QtQuick.Controls.Material 6.0
import QtQuick.Layouts 6.0

import "components"

ApplicationWindow {
    visible: true
    width: 856
    height: 482

    title: "ModVault"

    Material.theme: Material.Dark

    QtObject{
        id: funtions

        function switch_screen(screen_index) {
            stack_layout.currentIndex = screen_index
        }

        function toggle_menu() {
            menu_animation.running = true

            if (menu.width === 50) {
                menu_button.icon_source = "qrc:/menu_icons/menu_icons/menu_2.png"
            } else {
                menu_button.icon_source = "qrc:/menu_icons/menu_icons/menu.png"
            }
        }
    }

    Rectangle {
        id: main
        color: "#3d3d3d"

        anchors.fill: parent

        Rectangle {
            id: menu
            width: 50
            opacity: 0.478
            color: "#3d3d3d"
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.leftMargin: 0
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            clip: true

            PropertyAnimation {
                id: menu_animation
                target: menu

                property: "width"
                to: if (menu.width === 50)
                        return 300
                    else
                        return 50

                duration: 500
                easing.type: Easing.InOutQuint
            }

            MenuButton {
                id: menu_button
                onClicked: funtions.toggle_menu()
            }
        }
    }
}

Here is the qmake file that I use to build the app:

######################################################################
# Automatically generated by qmake (3.1) Wed May 3 17:38:34 2023
######################################################################

TEMPLATE = app
TARGET = ModVault
INCLUDEPATH += .

# You can make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# Please consult the documentation of the deprecated API in order to know
# how to port your code away from it.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_UP_TO=0x060000 # disables all APIs deprecated in Qt 6.0.0 and earlier

# Input
SOURCES += src/main.cpp
QT += quick
DESTDIR = ./build
RESOURCES += src/resources.qrc

Code for the qrc:

<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/components">
    <file>gui.qml</file>
    <file>components/MenuButton.qml</file>
</qresource>
<qresource prefix="/menu_icons">
    <file>menu_icons/menu.png</file>
    <file>menu_icons/menu_2.png</file>
    <file>menu_icons/home.png</file>
    <file>menu_icons/download.png</file>
    <file>menu_icons/github.png</file>
    <file>menu_icons/repository.png</file>
    <file>menu_icons/update.png</file>
    <file>menu_icons/close.png</file>
    <file>menu_icons/minimize.png</file>
    <file>menu_icons/settings.png</file>
</qresource>
</RCC>

What I Have Tried

Things that I have tried include:

  1. Use QQuickView instead but it didn't change anything
  2. Added visible: true, but no change
  3. Using qrc to load qml

Some other information: I am using make and qmake to build the app.

What should I do to fix this issue?


Solution

  • I finally found the answer, and it was completely my fault. Thank you to everyone who tried to help and sorry for wasting your time.

    Answer: When I first ran this app I got this error:

    "qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found."
    

    I googled and the first answer from a github issue was to set this environment variable:

    export QT_QPA_PLATFORM=offscreen
    

    Now I realised that this also stops the app from actually displaying. So to fix this I removed the environment variable and got the same error messages as before, so I just installed libxcb-cursor by doing this:

    sudo apt install libxcb-cursor
    

    So that was it.

    Again thank you to everyone who tried to help. Sorry for wasting your time.