EDIT
My CMake file looks like this:
cmake_minimum_required(VERSION 3.16)project(testqt VERSION 0.1 LANGUAGES CXX)set(CMAKE_AUTOMOC ON)set(CMAKE_CXX_STANDARD_REQUIRED ON)find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)qt_standard_project_setup()qt_add_executable(apptestqt main.cpp test.cpp test.hpp)qt_add_qml_module(apptestqt URI testqt VERSION 1.0 QML_FILES Main.qml)# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.# If you are developing for iOS or macOS you should consider setting an# explicit, fixed bundle identifier manually though.set_target_properties(apptestqt PROPERTIES# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.apptestqt MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE)target_link_libraries(apptestqt PRIVATE Qt6::Quick)include(GNUInstallDirs)install(TARGETS apptestqt BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
ORIGINAL QUESTION
I have simple testing code as follow:
test.hpp
#pragma once#include <QQmlEngine>class test_gadget { Q_GADGET QML_ELEMENT Q_PROPERTY(int test MEMBER test)public: int test;};class test_object : public QObject { Q_OBJECT QML_ELEMENTpublic: test_object(const test_gadget&);private: test_gadget gadget;};
test.cpp
#include <test.hpp>test_object::test_object(const test_gadget& other) { this->gadget = other;}
main.cpp
#include <QQmlContext>#include <test.hpp>int main(int argc, char *argv[]){ QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(u"qrc:/testqt/Main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,&app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); test_gadget gadget; test_object test_obj(gadget); engine.rootContext()->setContextProperty("test_obj", &test_obj); engine.load(url); return app.exec();}
However when I try to compile, there were errors:
[ 5%] Built target apptestqt_qmlimportscan[ 10%] Built target apptestqt_tooling[ 15%] Automatic MOC and UIC for target apptestqt[ 20%] Built target apptestqt_autogen[ 25%] Running AUTOMOC file extraction for target apptestqt[ 25%] Built target apptestqt_automoc_json_extraction[ 30%] Automatic QML type registration for target apptestqtError 5 while parsing C:/Users/USER/source/repos/build-testqt-Desktop_Qt_6_6_0_MinGW_64_bit-Debug/meta_types/qt6apptestqt_debug_metatypes.json: illegal valuemingw32-make.exe[2]: *** [CMakeFiles\apptestqt.dir\build.make:88: apptestqt_qmltyperegistrations.cpp] Error 1mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:97: CMakeFiles/apptestqt.dir/all] Error 2mingw32-make.exe: *** [Makefile:135: all] Error 213:22:22: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited with code 2.Error while building/deploying project testqt (kit: Desktop Qt 6.6.0 MinGW 64-bit)When executing step "Build"
Note:build-testqt-Desktop_Qt_6_6_0_MinGW_64_bit-Debug\meta_types\qt6apptestqt_debug_metatypes.json
is empty
How should I fix this?
Qt version: 6.6.0