Cross compilation of Qt6.11.1 and OpenCV For Raspberry pi 3/4/5 with Docker(Base and QML packages) and Remote Debugging with Vscode
In this content, you will find a way to cross-compile Qt 6.11.1 and Opencv (4.9.0) for Raspberry Pi hardware using Docker isolation.
This is a complete tutorial that you can learn how to debug the application with vscode.
The primary advantage of Docker is its ability to isolate the build environment. This means you can build Qt without needing a Raspberry Pi (real hardware) and regardless of your host OS type, as long as you can run Docker (along with QEMU). Additionally, you won’t need to handle dependencies anymore (and I’m not kidding). This approach is easier and less painful.
Watch the video for more details:

For opencv Compilation Opencv with Docker
For remote debugging and follow up click.
I tested this on Ubuntu 22 and 20. Regardless of the version, Qt is successfully compiled and builds a 'Hello World' application (with QML) for the Raspberry Pi.
The steps will show you how to prepare your build environment (in this case, Ubuntu) and run the Docker commands to build Qt 6.11.1. But as I mentioned, you don't need to use Ubuntu; as long as you can run the Docker engine and QEMU, you should achieve the same result on any platform.
If you want to check with virtual machine you can find tutorial Here. Steps are quite same, for this case you need raspberry pi. It is classical way that you can find in this repository. Or If you want more infromation, check old videos about it.
If you want to understand theory for cross complation of Qt for rasppberry pi without Docker in detail, you can watch this video which shows how to compile Qt 6.3.0 for raspberry pi(only toolchain is not compiled).
Install Docker
NOTE: If you see error during installation, then search on the internet how to install docker and qemu for your os. During time this steps can be different as you expect.
I have ubuntu 24 (according to ubuntu version steps can vary)
ulas@ulas:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble
Lets install dependencies.
# Add Docker's official GPG key:
$ sudo apt-get update
$ sudo apt-get install ca-certificates curl
$ sudo install -m 0755 -d /etc/apt/keyrings
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
$ sudo chmod a+r /etc/apt/keyrings/docker.asc
Set up stable repository for docker
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt-get update
Install related packages for Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify installation with hello-world image
$ sudo docker run hello-world
Lets manage user permission for Docker. Docker uses UDS so permission is needed.
$ sudo usermod -aG docker ${USER}
$ su - ${USER}
$ sudo systemctl enable docker
We also need to install QEMU, with it, it is possible to emulate/run raspbian os like it is on real raspberry pi 4 hardware
$ sudo apt install qemu-system-x86 qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
Enable and Start Libvirt:
$ sudo systemctl enable libvirtd
$ sudo systemctl start libvirtd
Add Your User to the Libvirt and KVM Groups:
$ sudo usermod -aG libvirt $(whoami)
$ sudo usermod -aG kvm $(whoami)
Verify Installation:
You should see an empty list.
Set up QEMU for multi-architecture support
$ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
Create and use a new Buildx builder instance
$ docker buildx create --use --name mybuilder
$ docker buildx inspect mybuilder --bootstrap
Verify Buildx installation
Compile Qt 6.11.1 with Docker
When I experimented with this idea, I expected to create a single Dockerfile with different stages, allowing me to switch between them even if they involved different hardware architectures. However, it didn't work as expected, so I ended up creating two separate Dockerfiles.
First, we will create a Raspbian (Debian-based) environment and emulate it. Then, we need to copy the relevant headers and libraries for later compilation
Run the command to create rasbian(debian) image.
$ docker buildx build --platform linux/arm64 --load -f DockerFileRasp -t raspimage .
When it finishes, you will find a file named 'rasp.tar.gz' in the '/build' directory within the image.
Let's copy it to the same location where the Dockerfile exists. Just copy it to where you pulled the branch.
To copy the file, you need to create a temporary container using the 'create' command. You can delete this temporary container later if you wish
$ docker create --name temp-arm raspimage
$ docker cp temp-arm:/build/rasp.tar.gz ./rasp.tar.gz
This rasp.tar.gz file will be copied by the another image that is why location of the tar file is important. You do not need to extract it. Do not touch it.
Now it is time to create debian trixie image and compile the Qt 6.11.1. Idea of the image selection is that if both images are match then it is not needed to compile specific toolchain. Because most of the time related cross toolchain is available in the repository. This process can be done using ubuntu as well but in this case spefici toolchain(gcc) should be compiled for the target. This will take time.
In one of the previous commands you used DockerFileRasp, this file is written for raspberry pi, now we are going to use only Dockerfile which is default name that means we do not need to specify path or name explicitly. But if you want you can change the name, you already now how you can pass the file name (with -f)
$ docker build -t qtcrossbuild .
As you see there is no buildx in this command because buildx uses qemu and we do not need qemu for x86 debian. After some time, ( I tested with 16GB RAM and it took around couple of hours) you see that image will be created without an error. After this, you can find HelloQt6 binary which is ready to run on Raspberry pi, in the /build/project directory in the image. So lets copy it. As we did before, you need to create temporary container to copy it.
$ docker create --name tmpbuild qtcrossbuild
$ docker cp tmpbuild:/build/project/HelloQt6 ./HelloQt6
As you see, example application is compiled for arm.
ulas@ulas:~/QTonRaspberryPi$ file HelloQt6
HelloQt6: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, with debug_info, not stripped
To test the hello world, you need to copy and send the compiled qt binaries in the image.
$ docker cp tmpbuild:/build/qt-pi-binaries.tar.gz ./qt-pi-binaries.tar.gz
$ scp qt-pi-binaries.tar.gz ulas@192.168.16.20:/home/ulas/
$ ssh rasp@192.168.16.25
$ ulas@raspberrypi:~ sudo mkdir /usr/local/qt6
$ ulas@raspberrypi:~ sudo tar -xvf qt-pi-binaries.tar.gz -C /usr/local/qt6
$ ulas@raspberrypi:~ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/qt6/lib/
Extract it under /usr/local or wherever you want and do not forget to add the path to LD_LIBRARY_PATH in case of path is not in the list.
ulas@raspberrypi:~ $ ./HelloQt6
Hello world
Compilation Opencv with Docker
Video:

To enable opencv (4.9.0) cross compilation during the x86 image creation, it is needed to make BUILD_OPENCV flag ON.
docker build --build-arg BUILD_OPENCV=ON -t qtcrossbuild .
This will compile the opencv with Qt. Qt will be compiled for default.
To copy opencv libraries you need to have container from this image like above. You can use the same container.
$ docker create --name tmpbuild qtcrossbuild
$ docker cp tmpbuild:/build/QtOpencvExample/build/QtOpencvHello ./QtOpencvHello
$ file QtOpencvHello
QtOpencvExample/build/QtOpencvHello: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (GNU/Linux), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=e846e63fec33e431bfdc1e70ad6d6b10c138992e, for GNU/Linux 3.7.0, not stripped
Then you can copy the tar file to raspberry pi.
$ docker cp tmpbuild:/build/opencv-binaries.tar.gz ./opencv-binaries.tar.gz
$ scp opencv-binaries.tar.gz ulas@192.168.16.20:/home/ulas/
$ scp QtOpencvHello ulas@192.168.16.20:/home/ulas/
$ ssh rasp@192.168.16.25
$ ulas@raspberrypi:~ sudo mkdir /usr/local/opencv
$ ulas@raspberrypi:~ sudo tar -xvf opencv-binaries.tar.gz -C /usr/local/opencv
$ ulas@raspberrypi:~ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv/lib/
When you run the application after setting path correctly. Note that you need to extract Qt binaries and export the path for Qt also, becuase example needs Qt and Opencv together.
Debugging of compilation
Nothing is free! Okay now we find a nice way to compile or build Qt applications but there is a tradeoff. Debugging is really hard. So If you want to change Dockerfile then first you sould build or test the steps on VM to be sure. If you know what you are doing then do not worry.
Each RUN commands output are printed in Build.log file that you can find in the build directory of image.
docker cp tmpbuild:/build.log ./build.log
Cross Development and Remote Debugging of Application with vscode
Detailed information:

Now, you can build your application by simply adding your files to the project directory and running the command:
$ docker build -t qtcrossbuild .
If you do not modify the Dockerfile, the above command will only compile the code. However, even if you don't alter the Dockerfile, if there is any update to Ubuntu, Docker will rebuild the image starting from the Ubuntu layer. This means it will take more time. To compile the Qt application you wish to develop, this is not necessary. There is another Dockerfile, Dockerfile.app, which allows you to compile only the application. If you examine the contents of this file, you will find:
This indicates that if you build an image with Dockerfile.app, it will utilize the qtcrossbuild image. There's no need to run qtcrossbuild again.
If you run:
$ docker build -f Dockerfile.app -t final-app .
With the final-app image, you can create a container solely for compilation purposes. Docker caches the previous commands, so when you run this command, it will not start from scratch but only execute the latest command where you wish to compile your application. The compilation process will begin in the image, then, as before, create a temporary container and copy your binary:
$ docker create --name tmpapp final-app
$ docker cp tmpapp:/projectPath/HelloQt6 ./HelloQt6
If you do not want to use the cache, or if you want to start building the same image anew, use:
$ docker build -t qtcrossbuild . --no-cache
However, if you prefer not to follow these steps, I have shared the tar files that I compiled for the Raspberry Pi, along with the related sysroot and toolchain. You can download them. In this case, you will need to have the correct dependencies. It's your choice.
I assume you have vscode already, we need some dependencies on the host for remote debugging with vscode.
$ sudo apt-get install sshpass gdb-multiarch