Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. the custom type from C++ is not visible in QML, but project compiles and works as expected
Forum Updated to NodeBB v4.3 + New Features

the custom type from C++ is not visible in QML, but project compiles and works as expected

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 464 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Anton1978A Offline
    Anton1978A Offline
    Anton1978
    wrote on last edited by
    #1

    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_H
    

    qml 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?

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #4

      My bad. You have to import the URI, so it should actually be

      import Test03
      

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      0
      • Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on last edited by
        #2

        The CMake file adds filecalculator.h and filecalculator.cpp to appTest03.
        That's why they compile. The import statement has to be:

        import appTest03
        

        Software Engineer
        The Qt Company, Oslo

        1 Reply Last reply
        0
        • Anton1978A Offline
          Anton1978A Offline
          Anton1978
          wrote on last edited by
          #3

          if - import appTest03 - it doesn't compile, error - QML module NOT found

          1 Reply Last reply
          0
          • Axel SpoerlA Offline
            Axel SpoerlA Offline
            Axel Spoerl
            Moderators
            wrote on last edited by
            #4

            My bad. You have to import the URI, so it should actually be

            import Test03
            

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            0
            • Anton1978A Anton1978 has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved