Skip to content

Commit

Permalink
Correct qml6-subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
herzenschein committed Jun 9, 2024
1 parent f2d9432 commit 355a084
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 82 deletions.
22 changes: 11 additions & 11 deletions qml6-subdirectories/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 1 addition & 9 deletions qml6-subdirectories/main.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>

// This include is required to use the following plugin macros
#include <QtQml/qqmlextensionplugin.h>
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();
}
25 changes: 5 additions & 20 deletions qml6-subdirectories/sub1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,8 +16,8 @@ ApplicationWindow {

Label {
Layout.alignment: Qt.AlignCenter
Layout.topMargin: 15
text: "This window comes from <i>sub1/main.qml</i>"
Layout.topMargin: 30
text: "This window comes from <i>sub1/Main.qml</i>"
}

Item {
Expand All @@ -34,6 +32,7 @@ ApplicationWindow {

TextRow {
Layout.alignment: Qt.AlignCenter
Layout.fillWidth: true
labelText: Strings.textForLabel
fieldPlaceholder: Strings.textForField
}
Expand Down
23 changes: 4 additions & 19 deletions qml6-subdirectories/sub1/sub2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions qml6-subdirectories/sub1/sub2/TextRow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Frame {
id: label
}
TextField {
Layout.fillWidth: true
id: field
}
}
Expand Down
20 changes: 3 additions & 17 deletions qml6-subdirectories/sub1/sub2/sub3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit 355a084

Please sign in to comment.