Qt Application Fails to Load QML Modules in Docker Container
-
Hello Qt Community,
I’m encountering several issues while running a Qt QML application inside a Docker container. The application starts but fails to load QML modules and the required Qt platform plugin. I’m looking for guidance on how to resolve these issues.
Issue Description
When running my Qt QML application in a Docker container, I get the following errors:Application starting... qt.qpa.xcb: could not connect to display :0 qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.
Dockerfile Configuration
Here’s the Dockerfile I’m using to build the Docker image:# Use an Ubuntu base image FROM ubuntu:22.04 # Install required dependencies RUN apt-get update && \ apt-get install -y \ build-essential \ cmake \ qtbase5-dev \ qtdeclarative5-dev \ qtquickcontrols2-5-dev \ libgl1-mesa-dev \ libxcb1 \ libxcb-xinerama0 \ libxcb-xkb1 \ libxkbcommon-x11-0 \ libxkbcommon0 \ libgl1-mesa-glx \ libgl1-mesa-dri \ && rm -rf /var/lib/apt/lists/* # Set environment variables for Qt ENV QT_QPA_PLATFORM_PLUGIN_PATH=/usr/lib/x86_64-linux-gnu/qt5/plugins/platforms # Create an application directory WORKDIR /usr/src/app # Copy your Qt QML application to the container COPY . /usr/src/app # Build the QML application using CMake RUN mkdir build && cd build && cmake .. && make # Command to run the QML application CMD ["./build/Qml_App"]
CPP main
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QDebug> int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif qDebug() << "Application starting..."; QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); qDebug() << "Loading QML file from" << url.toString(); QObject::connect( &engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) { qDebug() << "Failed to load QML file"; QCoreApplication::exit(-1); } }, Qt::QueuedConnection); engine.load(url); qDebug() << "Application loaded"; return app.exec(); }
Troubleshooting Steps Taken
-
Verified Installation of Qt Modules: Ensured the Dockerfile installs all necessary Qt packages.
-
Checked Qt Plugin Path: Set QT_QPA_PLATFORM_PLUGIN_PATH in the Dockerfile.
-
Verified QML Imports: Confirmed that the QML modules should be present in the container.
-
Verified Plugin Path: Ensured the Qt platform plugin path is correctly set.
Request for Assistance
-
Ensuring the required QML modules are correctly installed in the Docker container.
-
Why Window is not opening(QML)
-
Diagnosing why the Qt platform plugin "xcb" cannot be initialized.
Thank you in advance for your help!
Best regards,
Rakesh U -
-
@Rakesh-U said in Qt Application Fails to Load QML Modules in Docker Container:
qt.qpa.xcb: could not connect to display :0
This is the only message which matters. However you invoke your application with the Docker Container you do not have an Xorg display available to it. Are you running an X server? Try to run some other Xorg application, such as
xterm
, and you should be seeing the same error.