diff --git a/qml6-subdirectories/CMakeLists.txt b/qml6-subdirectories/CMakeLists.txt index 771818c..1c30af1 100644 --- a/qml6-subdirectories/CMakeLists.txt +++ b/qml6-subdirectories/CMakeLists.txt @@ -6,27 +6,27 @@ find_package(Qt6 REQUIRED COMPONENTS Quick) qt_standard_project_setup() -# This creates our qml6-subdirectories target -qt_add_executable(${PROJECT_NAME} main.cpp) - -# This defines our custom output directory as -# a qml/ folder for each plugin in our build/ folder -# This is the same as directly adding OUTPUT_DIRECTORY to each QML module! -set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml) - # This silences a warning about not using the new default :/qt/qml resource prefix path # https://doc.qt.io/qt-6/qt-cmake-policy-qtp0001.html qt_policy(SET QTP0001 NEW) +# This creates our qml6-subdirectories target +qt_add_executable(${PROJECT_NAME}) + +target_sources(${PROJECT_NAME} + PRIVATE + "main.cpp" +) + # This links the required QtQuick libraries # as well as our static QML plugins # set in the subdirectory CMakeLists.txt files # to be used in our import statements target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Quick - mainplugin - textrowplugin - stringsplugin + subdirectories-main + subdirectories-textrow + subdirectories-strings ) add_subdirectory(sub1) diff --git a/qml6-subdirectories/main.cpp b/qml6-subdirectories/main.cpp index d078538..587fbeb 100644 --- a/qml6-subdirectories/main.cpp +++ b/qml6-subdirectories/main.cpp @@ -1,20 +1,12 @@ #include #include -// This include is required to use the following plugin macros -#include -Q_IMPORT_QML_PLUGIN(MainPlugin) -Q_IMPORT_QML_PLUGIN(TextRowPlugin) -Q_IMPORT_QML_PLUGIN(StringsPlugin) - int main (int argCount, char* argVector[]) { QGuiApplication subdirectoryExampleApp(argCount, argVector); QQmlApplicationEngine engine; - // The import path is needed in case a custom output directory is set - engine.addImportPath("qrc:/"); - engine.load("qrc:/Main/main.qml"); + engine.loadFromModule("com.example.subdirectories.main", "Main"); subdirectoryExampleApp.exec(); } diff --git a/qml6-subdirectories/sub1/CMakeLists.txt b/qml6-subdirectories/sub1/CMakeLists.txt index 7841887..0bab3f3 100644 --- a/qml6-subdirectories/sub1/CMakeLists.txt +++ b/qml6-subdirectories/sub1/CMakeLists.txt @@ -1,25 +1,10 @@ -# This ensures CMake will search this folder -set(CMAKE_INCLUDE_CURRENT_DIR ON) +qt_add_library(subdirectories-main) -# This is exactly the same thing as in sub2/CMakeLists.txt -# So use either qt_add_library or STATIC in the qt_add_qml_module. - -#qt_add_library(main STATIC) - -# Our QML files that are going to be -# shipped with our application need to be static. -# Creating the target "main" here -# (also called a module) -# autogenerates a mainplugin that can be used in -# the root target_link_libraries(). -qt_add_qml_module(main - URI "Main" +qt_add_qml_module(subdirectories-main + URI "com.example.subdirectories.main" VERSION 1.0 - RESOURCE_PREFIX "/" - # OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml - # OUTPUT_DIRECTORY "Main" - QML_FILES "main.qml" - STATIC + OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/com/example/subdirectories/main" + QML_FILES "Main.qml" ) add_subdirectory(sub2) diff --git a/qml6-subdirectories/sub1/main.qml b/qml6-subdirectories/sub1/Main.qml similarity index 80% rename from qml6-subdirectories/sub1/main.qml rename to qml6-subdirectories/sub1/Main.qml index 9eb7151..8c7c6ba 100644 --- a/qml6-subdirectories/sub1/main.qml +++ b/qml6-subdirectories/sub1/Main.qml @@ -3,10 +3,8 @@ import QtQuick.Controls import QtQuick.Controls.Material import QtQuick.Layouts -// I don't get why QtCreator doesn't find these modules, -// but it works. -import TextRow -import Strings +import com.example.subdirectories.textrow +import com.example.subdirectories.strings ApplicationWindow { title: "Minimal QML6 Subdirectories Example" @@ -18,8 +16,8 @@ ApplicationWindow { Label { Layout.alignment: Qt.AlignCenter - Layout.topMargin: 15 - text: "This window comes from sub1/main.qml" + Layout.topMargin: 30 + text: "This window comes from sub1/Main.qml" } Item { @@ -34,6 +32,7 @@ ApplicationWindow { TextRow { Layout.alignment: Qt.AlignCenter + Layout.fillWidth: true labelText: Strings.textForLabel fieldPlaceholder: Strings.textForField } diff --git a/qml6-subdirectories/sub1/sub2/CMakeLists.txt b/qml6-subdirectories/sub1/sub2/CMakeLists.txt index bb5ed14..0211e10 100644 --- a/qml6-subdirectories/sub1/sub2/CMakeLists.txt +++ b/qml6-subdirectories/sub1/sub2/CMakeLists.txt @@ -1,25 +1,10 @@ -# This ensures CMake will search this folder -set(CMAKE_INCLUDE_CURRENT_DIR ON) +qt_add_library(subdirectories-textrow) -# This is exactly the same thing as in sub2/CMakeLists.txt -# So use either qt_add_library or STATIC in the qt_add_qml_module. - -qt_add_library(textrow STATIC) - -# Our QML files that are going to be -# shipped with our application need to be static. -# Creating the target "main" here -# (also called a module) -# autogenerates a mainplugin that can be used in -# the root target_link_libraries(). -qt_add_qml_module(textrow - URI "TextRow" +qt_add_qml_module(subdirectories-textrow + URI "com.example.subdirectories.textrow" VERSION 1.0 - RESOURCE_PREFIX "/" - # OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml - # OUTPUT_DIRECTORY "TextRow" + OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/com/example/subdirectories/textrow QML_FILES "TextRow.qml" -# STATIC ) add_subdirectory(sub3) diff --git a/qml6-subdirectories/sub1/sub2/TextRow.qml b/qml6-subdirectories/sub1/sub2/TextRow.qml index 6d08ffa..1a2a07f 100644 --- a/qml6-subdirectories/sub1/sub2/TextRow.qml +++ b/qml6-subdirectories/sub1/sub2/TextRow.qml @@ -13,6 +13,7 @@ Frame { id: label } TextField { + Layout.fillWidth: true id: field } } diff --git a/qml6-subdirectories/sub1/sub2/sub3/CMakeLists.txt b/qml6-subdirectories/sub1/sub2/sub3/CMakeLists.txt index 10d7c1e..b8ee9d5 100644 --- a/qml6-subdirectories/sub1/sub2/sub3/CMakeLists.txt +++ b/qml6-subdirectories/sub1/sub2/sub3/CMakeLists.txt @@ -1,22 +1,8 @@ -# This ensures CMake will search this folder -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -# This add new source files to the desired target, namely our executable. -# Because this is a single header file, we can set its visibility to INTERFACE. -# The main effect of this is the file appearing in the Projects tab in IDEs. -target_sources(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/strings.hpp) - -# Like the previous CMakeLists.txt files, -# either method for setting STATIC works. - -#qt_add_library(strings STATIC) +qt_add_library(subdirectories-strings) qt_add_qml_module(strings - URI "Strings" + URI "com.example.subdirectories.strings" VERSION 1.0 - RESOURCE_PREFIX "/" - # OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml - # OUTPUT_DIRECTORY "Strings" + OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/com/example/subdirectories/strings" SOURCES "strings.hpp" - STATIC )