Skip to content

Instantly share code, notes, and snippets.

@tobiasBora
Last active June 5, 2024 18:23
Show Gist options
  • Save tobiasBora/04d0febda0b3f09707b5e1b7b85390a5 to your computer and use it in GitHub Desktop.
Save tobiasBora/04d0febda0b3f09707b5e1b7b85390a5 to your computer and use it in GitHub Desktop.
Qt5: QML hello world with Cmake (+nix package manager)

Demonstration of QML with Qt5 + Cmake + nix

If you have nix installed compile and run with:

$ nix build
$ ./result/bin/demo 

Explaination: we load the .qml file with qrc:///main.qml: it uses for that the file qml.qrc (the name is not important) that lists the resource files. This 'qml.qrc file is then given to qt5_add_resources(…) in CMakeLists.txt in order to compile the resources and include them in the final binary. Note that resources must be compiled: other methods involve qmake or a manual compilation.

cmake_minimum_required(VERSION 3.1.0)
project(demo VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
# Add here any library you need
find_package(Qt5 COMPONENTS Widgets Qml Quick REQUIRED)
# Add the resource file as it *must* be compiled (also possible with
# qmake or manually with rcc)
# https://doc.qt.io/qt-5/resources.html
# https://doc.qt.io/qt-5/qtcore-cmake-qt5-add-resources.html
set(SOURCES main.cpp) # A variable is needed as I guess it will add more sources inside
qt5_add_resources(SOURCES qml.qrc)
add_executable(demo
${SOURCES}
)
# Add here any library you need
target_link_libraries(demo Qt5::Widgets Qt5::Qml Qt5::Quick)
# Install the binary
install(TARGETS demo DESTINATION bin)
{ lib
, stdenv
, cmake
, qt5
, wrapQtAppsHook
}:
stdenv.mkDerivation rec {
pname = "";
version = "";
src = ./.;
nativeBuildInputs = [ cmake wrapQtAppsHook ];
buildInputs = [
qt5.qtbase
];
}
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1668650906,
"narHash": "sha256-JuiYfDO23O8oxUUOmhQflmOoJovyC5G4RjcYQMQjrRE=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "3a86856a13c88c8c64ea32082a851fefc79aa700",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}
{
description = "A very basic flake";
outputs = { self, nixpkgs }: let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in {
packages.x86_64-linux.default = pkgs.libsForQt5.callPackage ./derivation_demo.nix {};
};
}
//https://riptutorial.com/qml/example/14475/creating-a-qtquick-window-from-cplusplus
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
import QtQuick 2.5
import QtQuick.Window 2.2
Window { // Must be this type to be loaded by QQmlApplicationEngine.
visible: true
title: qsTr("Hello World")
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment