From 4da9a275a900870d93276c443a9c0ae195641a8b Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 22 Nov 2023 20:13:33 -0300 Subject: [PATCH] initial setup --- CMakeLists.txt | 72 ++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 31 ++++++++++++++++++++ main_web.cpp | 25 ++++++++++++++++ qml/CMakeLists.txt | 11 +++++++ qml/main.qml | 59 +++++++++++++++++++++++++++++++++++++ root.cpp | 27 +++++++++++++++++ root.h | 13 +++++++++ 7 files changed, 238 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.cpp create mode 100644 main_web.cpp create mode 100644 qml/CMakeLists.txt create mode 100644 qml/main.qml create mode 100644 root.cpp create mode 100644 root.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d1c3393 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,72 @@ +cmake_minimum_required(VERSION 3.21.1) + +project(megpie + VERSION 1.0 + LANGUAGES CXX +) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(Qt6 6.6 REQUIRED COMPONENTS Core Gui Qml Quick QuickControls2 QuickLayouts) + +qt_standard_project_setup() + +qt_policy(SET QTP0001 NEW) + +if(NOT EMSCRIPTEN AND CMAKE_TOOLCHAIN_FILE MATCHES ".*Emscripten\.cmake$") + set(EMSCRIPTEN TRUE) +endif() + +qt_add_executable(megpie + root.cpp +) + +if (NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Debug) +endif () +message("Build type: ${CMAKE_BUILD_TYPE}") + +if (EMSCRIPTEN) + if (CMAKE_BUILD_TYPE STREQUAL Debug) + set(CMAKE_CXX_FLAGS "-gsource-map") + else() + target_compile_options(megpie PRIVATE -Oz -g0) + target_link_options(megpie PRIVATE -0z -g0 --closure 1) + endif() + message("building for emscripten") + target_sources(megpie PRIVATE main_web.cpp) +else() + if (CMAKE_BUILD_TYPE STREQUAL Debug) + else() + target_compile_options(megpie PRIVATE -O3) + endif() + message("building for desktop") + target_sources(megpie PRIVATE main.cpp) +endif() + +if (CMAKE_BUILD_TYPE STREQUAL Debug) +else() + target_compile_options(megpie PRIVATE -flto) + target_link_options(megpie PRIVATE -flto) +endif() + +add_subdirectory(qml) + +target_link_libraries(megpie PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Qml + Qt6::Quick + Qt6::QuickControls2 + Qt6::QuickLayouts +) + +include(GNUInstallDirs) +install(TARGETS megpie + BUNDLE DESTINATION . + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4ed2fc8 --- /dev/null +++ b/main.cpp @@ -0,0 +1,31 @@ +#include +#include + +#include "root.h" + +int main(int argc, char *argv[]) { + Root app(argc, argv); + + std::cout << "Starting" << std::endl; + QQmlApplicationEngine engine; + const QUrl url(u"qrc:qml/main.qml"_qs); + QObject::connect( + &engine, &QQmlApplicationEngine::objectCreated, &app, + [&url](QObject* obj, const QUrl& objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-2); + }, + Qt::QueuedConnection); + + engine.load(url); + if (engine.rootObjects().isEmpty()) + return -1; + + int result; + try { + result = app.exec(); + } catch (...) { + } + + return result; +} diff --git a/main_web.cpp b/main_web.cpp new file mode 100644 index 0000000..a37688c --- /dev/null +++ b/main_web.cpp @@ -0,0 +1,25 @@ +#include +#include + +#include "root.h" + +int main(int argc, char *argv[]) { + Root* app = new Root(argc, argv); + + std::cout << "Starting" << std::endl; + QQmlApplicationEngine* engine = new QQmlApplicationEngine(); + const QUrl url(u"qrc:qml/main.qml"_qs); + QObject::connect( + engine, &QQmlApplicationEngine::objectCreated, app, + [url](QObject* obj, const QUrl& objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-2); + }, + Qt::QueuedConnection); + + engine->load(url); + if (engine->rootObjects().isEmpty()) + return -1; + + return 0; +} diff --git a/qml/CMakeLists.txt b/qml/CMakeLists.txt new file mode 100644 index 0000000..450ac5d --- /dev/null +++ b/qml/CMakeLists.txt @@ -0,0 +1,11 @@ +qt_add_qml_module(megpieQml + URI "qml" + VERSION 1.0 + STATIC + RESOURCE_PREFIX / + NO_PLUGIN + QML_FILES + main.qml +) + +target_link_libraries(megpie PRIVATE megpieQml) diff --git a/qml/main.qml b/qml/main.qml new file mode 100644 index 0000000..080e1f2 --- /dev/null +++ b/qml/main.qml @@ -0,0 +1,59 @@ +import QtQuick +import QtQuick.Window +import QtQuick.Controls +import QtQuick.Layouts + +Window { + property int counter: 0 + + id: window + width: 640 + height: 480 + visible: true + title: qsTr("Hello World") + + Rectangle { + id: page + anchors.fill: parent + anchors.centerIn: parent + color: increment.down ? "blue" : decrement.down ? "red" : "lightgrey" + + Behavior on color { + ColorAnimation { + duration: 100 + target: page + easing.type: Easing.InOutQuad + } + } + + GridLayout { + anchors.centerIn: parent + columns: 2 + + Text { + Layout.columnSpan: 2 + Layout.alignment: Qt.AlignHCenter + text: qsTr("Hello World") + font.pointSize: 24; font.bold: true + } + + Button { + id: increment + text: "Increment" + onClicked: window.counter++ + } + + Button { + id: decrement + text: "Decrement" + onClicked: window.counter-- + } + + Text { + Layout.columnSpan: 2 + Layout.alignment: Qt.AlignHCenter + text: "The value is " + window.counter + } + } + } +} diff --git a/root.cpp b/root.cpp new file mode 100644 index 0000000..4e173f2 --- /dev/null +++ b/root.cpp @@ -0,0 +1,27 @@ +#include "root.h" + +Root::Root(int& argc, char* argv[]) : + QGuiApplication(argc, argv) +{ + +} + +Root::~Root() { + +} + +bool Root::notify(QObject* receiver, QEvent* e) { + try { + return QGuiApplication::notify(receiver, e); + } catch (const std::exception& e) { + std::cout << "Caught an exception, error message:\n" << e.what() << std::endl; + } catch (const int& e) { + std::cout << "Caught int exception: " << e << std::endl; + } catch (...) { + std::cout << "Caught an exception" << std::endl; + } + + std::cout << "Squawk is quiting..." << std::endl; + exit(1); + return false; +} diff --git a/root.h b/root.h new file mode 100644 index 0000000..27eafcc --- /dev/null +++ b/root.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +#include + +class Root : public QGuiApplication { + Q_OBJECT +public: + Root(int& argc, char* argv[]); + ~Root(); + bool notify(QObject* receiver, QEvent* e) override; +};