Skip to content

Instantly share code, notes, and snippets.

@tobiasBora
Last active November 18, 2022 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasBora/812701e8741814393f3df7b23a11eb4b to your computer and use it in GitHub Desktop.
Save tobiasBora/812701e8741814393f3df7b23a11eb4b to your computer and use it in GitHub Desktop.
Qt5: QML hello world with Cmake (+nix package manager)

Demonstration of QML with Qt5 + meson + 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 resources in meson.build in order to compile the resources and include them in the final binary.

{ lib
, stdenv
, meson
, qt5
, wrapQtAppsHook
, pkg-config
, ninja
}:
stdenv.mkDerivation rec {
pname = "";
version = "";
src = ./.;
nativeBuildInputs = [ meson ninja pkg-config wrapQtAppsHook ];
buildInputs = [
qt5.full
];
}
{
"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
}
}
project('demo', 'cpp')
qt5 = import('qt5')
qt5_dep = dependency('qt5', modules: ['Core', 'Gui', 'Qml'])
resources = qt5.compile_resources(name: 'main.cpp', sources: [ 'qml.qrc' ])
executable('demo', 'main.cpp', resources,
dependencies : qt5_dep,
install : true)
<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