QML Multiple modules
-
I am attempting to follow the blog posts https://www.qt.io/blog/introduction-to-the-qml-cmake-api and https://www.qt.io/blog/qml-modules-in-qt-6.2 and use the Qt6 "approved" process and not do the registrations we did in Qt5.
Edit:
As I've been reading and re-reading the posts I think what I want is in the first blog (Intro to cmake api) because I want to create a QML application that produces an application exe that can be run.I also want to be able to use the C++ properties, signals, slots, and routines in my QML modules so to that they need to either be part of the current QML module URI which is FlightLog or I need to be able to do an import thecppmodule uri.
End Edit.
I have the following structure
Project Directory
CMakeLists.txt
main.cpp
main.qml
importdata (directory)
CMakeLists.txt
DataProcess.qml
importdata.h
importdata.cppI have tried each blog's process but my DataProcess is not recognized if I try and use it in main.qml. According to this post one I set things up in the CMakeLists.txt as they tell me I should be able to import my module as import ImportModule in main.qml.
Qt has lots of documentation but sometimes they assume we know what we are doing and the examples leave out intermediates steps that are not obvious. For example, I assume the Q_IMPORT_QML_PLUGIN goes where I have it - after the application engine starts but that's not specified - docs just say add.
These are from the second blog with multiple modules.
Thank you for any help.
main.cpp
//Qt Includes #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QtQml/QQmlExtensionPlugin> // Our includes int main(int argc, char *argv[]) { QCoreApplication::setOrganizationName("Dakota Pilot"); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; // Register our plugins Q_IMPORT_QML_PLUGIN(ImportModulePlugin) const QUrl url(u"qrc:/FlightLog/main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
Main project CMakeLists.txt
cmake_minimum_required(VERSION 3.16) project(FlightLog VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Set application name for use in building. set(appName "FlightLogbook") find_package(Qt6 COMPONENTS Quick REQUIRED) #Added these to finds hoping it would help. # Core is probably redundant due to Quick find find_package(Qt6 COMPONENTS Core REQUIRED) find_package(Qt6 COMPONENTS Qml REQUIRED) # Standard project setup #qt_standard_project_setup() # The executable qt_add_executable(FlightLogbook main.cpp ) add_subdirectory( importdata ) # QML and other files qt_add_qml_module(FlightLogbook URI FlightLog VERSION 1.0 QML_FILES main.qml DataImport.qml ) set_target_properties(FlightLogbook PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) # Added Core, Qml as private libraries target_link_libraries(FlightLogbook PRIVATE Qt6::Core PRIVATE Qt6::Quick PRIVATE Qt6::Qml) # Our libraries target_link_libraries(FlightLogbook PRIVATE import_moduleplugin) # Attempting to get a build that runs stanalone #qt_generate_deploy_qml_app_script( # TARGET FlightLogbook # FILENAME_VARIABLE deploy_script # DEPLOY_USER_QML_MODULES_ON_UNSUPPORTED_PLATFORM # NO_UNSUPPORTED_PLATFORM_ERROR # MACOS_BUNDLE_POST_BUILD #) # Attempting to put the runtime result in the directory above the build install(TARGETS FlightLogbook BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION .. ) # This is supposed to run the deploy script to create a standalone in the directory given above. #install(SCRIPT ${deploy_script})
importdata CMakeLists.txt
# CMakeLists.txt to build import backend. qt_add_library(import_module STATIC) qt6_add_qml_module(import_module URI ImportModule VERSION 1.0 QML_FILES DataProcess.qml SOURCES importdata.h importdata.cpp )
-
I finally got it working. I am not sure what made things start working but here is what I now have. I can now instantiate DataProcess in my main.qml and it will respond to signals from main.qml and do it's thing.
Project root (directory)
CMakeLists.txt
main.qml
main.cpp
importdata (directory)
CMakeLists.txt
DataProcess.qml
importdata.h
import data.cppThe main CMakeLists.txt has the following added.
add_subdirectory(importdata)
target_link_libraries(FlightLogbook PRIVATE import_moduleplugin)The importdata CMakeLists.txt is
# CMakeLists.txt to build import backend. qt_add_library(import_module STATIC) qt6_add_qml_module(import_module URI importdata VERSION 1.0 QML_FILES DataProcess.qml SOURCES importdata.h importdata.cpp )
in main.cpp I added these items.
#include <QtQml/QQmlExtensionPlugin>
// Plugins
Q_IMPORT_QML_PLUGIN(importdataPlugin)
before the int main(...)In main.qml I could then do
import importdataand in the body
DataProcess {
id: someid
}