the custom type from C++ is not visible in QML, but project compiles and works as expected
-
another problem:
the custom type from C++ is not visible in QML and causes an error, although the project compiles and works as expected
Here's the whole project:cmake_minimum_required(VERSION 3.16) project(Test03 VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.5 REQUIRED COMPONENTS Quick QuickControls2) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(appTest03 main.cpp ) qt_add_qml_module(appTest03 URI Test03 VERSION 1.0 QML_FILES Main.qml RESOURCES QML_FILES SOURCES filecalculator.h filecalculator.cpp ) # 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(appTest03 PROPERTIES # MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appTest03 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(appTest03 PRIVATE Qt6::Quick Qt6::QuickControls2 ) include(GNUInstallDirs) install(TARGETS appTest03 BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )c++ header
#ifndef FILECALCULATOR_H #define FILECALCULATOR_H #include <QObject> #include <qqml.h> class FileCalculator : public QObject { Q_OBJECT Q_PROPERTY(int calc READ calc WRITE setCalc NOTIFY calcChanged FINAL) QML_ELEMENT public: explicit FileCalculator(); int calc() const; void setCalc(int newCalc); Q_INVOKABLE void setFile(QString file); signals: void calcChanged(); private: int m_calc = 0; QString _fileName = ""; }; #endif // FILECALCULATOR_Hqml file
import QtQuick import QtQuick.Layouts import QtQuick.Controls 2.4 //import FileCalculator //error - it doesn’t compile, but if you remove the line then everything is fine Window { id: windowID visible: true maximumHeight : 500 maximumWidth : 500 minimumHeight : 500 minimumWidth : 500 FileCalculator{ //error - but if you ignore it, it will compile and everything works as it should, why the error? id: fcID } Column{ anchors.centerIn: parent spacing: 25 Rectangle{ id: monitorID color: "lightblue" width: 250 height: 50 radius: 10 Text{ anchors.centerIn: parent text: fcID.calc } } Rectangle{ id: filedownloaderID color: "lightgreen" width: 250 height: 50 radius: 10 Text{ id: fdtID anchors.centerIn: parent text: "Drag the file here" } DropArea{ id: dropAreaID anchors.fill: parent onDropped: function(drop) { console.log(drop.urls[0]); fcID.setFile(drop.urls[0]); filedownloaderID.color = "lightgray"; fdtID.text = "File selected" dropAreaID.enabled = false } } } } }why - FileCalculator{ //error - but if you ignore it, it will compile and everything works as it should, why the error?
-
My bad. You have to import the URI, so it should actually be
import Test03 -
The CMake file adds
filecalculator.handfilecalculator.cpptoappTest03.
That's why they compile. The import statement has to be:import appTest03 -
My bad. You have to import the URI, so it should actually be
import Test03 -
A Anton1978 has marked this topic as solved on