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. How to register and access C++ class methods in QML
Forum Update on Monday, May 27th 2025

How to register and access C++ class methods in QML

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 3 Posters 3.4k Views
  • 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.
  • S Offline
    S Offline
    Sushma_MP
    wrote on last edited by A Former User
    #1

    Hi ,
    Am trying to register my cpp class into QML by using qmlRegisterType and wanted to access methods present in cpp file in my main.qml .
    As per my code i have two methods test() and demo() i am able to acces only test method in main.qml but not able to access demo() method.

    can we use Q_INVOKABLE for more than one methods...????

    handlecppclass.cpp

    
            
            #include "handlecppclass.h"
            
            HandleCppClass::HandleCppClass(QObject *parent) : QObject(parent)
            {
            
            }
            
            void HandleCppClass::test()
            {
                qDebug()<<"inside test slot"<<endl;
            }
            
            void HandleCppClass::demo()
            {
                qDebug()<<"inside demo slot"<<endl;
            }
    
    

    handlecppclass.h

    
            
            #ifndef HANDLECPPCLASS_H
            #define HANDLECPPCLASS_H
            
            #include <QObject>
            #include <QDebug>
            
            class HandleCppClass : public QObject
            {
                Q_OBJECT
            public:
                explicit HandleCppClass(QObject *parent = 0);
            
            
            signals:
            
            public slots:
                Q_INVOKABLE void test();
                Q_INVOKABLE void demo();
            
            };
            
            #endif // HANDLECPPCLASS_H
    

    main.cpp

     
            
            #include <QGuiApplication>
            #include <QQmlApplicationEngine>
            
            #include <handlecppclass.h>
            
            int main(int argc, char *argv[])
            {
                QGuiApplication app(argc, argv);
                qmlRegisterType<HandleCppClass>("com.cppclass",1,0,"HandleCppClass");
            
                QQmlApplicationEngine engine;
                engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            
                return app.exec();
            }
    

    main.qml

     
            
            import QtQuick 2.7
            import QtQuick.Window 2.2
            import com.cppclass 1.0
            
            Window {
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")
            
                HandleCppClass{
                    id : classhandle
                }
            
                MouseArea{
                    id: mouse
                    anchors.fill: parent
                    onClicked: {
                        classhandle.test()
            
                    }
                }
            }
    
    raven-worxR 1 Reply Last reply
    0
    • S Sushma_MP

      Hi ,
      Am trying to register my cpp class into QML by using qmlRegisterType and wanted to access methods present in cpp file in my main.qml .
      As per my code i have two methods test() and demo() i am able to acces only test method in main.qml but not able to access demo() method.

      can we use Q_INVOKABLE for more than one methods...????

      handlecppclass.cpp

      
              
              #include "handlecppclass.h"
              
              HandleCppClass::HandleCppClass(QObject *parent) : QObject(parent)
              {
              
              }
              
              void HandleCppClass::test()
              {
                  qDebug()<<"inside test slot"<<endl;
              }
              
              void HandleCppClass::demo()
              {
                  qDebug()<<"inside demo slot"<<endl;
              }
      
      

      handlecppclass.h

      
              
              #ifndef HANDLECPPCLASS_H
              #define HANDLECPPCLASS_H
              
              #include <QObject>
              #include <QDebug>
              
              class HandleCppClass : public QObject
              {
                  Q_OBJECT
              public:
                  explicit HandleCppClass(QObject *parent = 0);
              
              
              signals:
              
              public slots:
                  Q_INVOKABLE void test();
                  Q_INVOKABLE void demo();
              
              };
              
              #endif // HANDLECPPCLASS_H
      

      main.cpp

       
              
              #include <QGuiApplication>
              #include <QQmlApplicationEngine>
              
              #include <handlecppclass.h>
              
              int main(int argc, char *argv[])
              {
                  QGuiApplication app(argc, argv);
                  qmlRegisterType<HandleCppClass>("com.cppclass",1,0,"HandleCppClass");
              
                  QQmlApplicationEngine engine;
                  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              
                  return app.exec();
              }
      

      main.qml

       
              
              import QtQuick 2.7
              import QtQuick.Window 2.2
              import com.cppclass 1.0
              
              Window {
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Hello World")
              
                  HandleCppClass{
                      id : classhandle
                  }
              
                  MouseArea{
                      id: mouse
                      anchors.fill: parent
                      onClicked: {
                          classhandle.test()
              
                      }
                  }
              }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Sushma_MP said in How to register and acess cpp class methods in QML:

      As per my code i have two methods test() and demo() i am able to acces only test method in main.qml but not able to access demo() method.

      what is the exact error you receive?

      Also you do not need to make slots invokable. Since they already are implicitly invokable.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sushma_MP
        wrote on last edited by
        #3

        there is no errors the thing is i am not able to access all methods which are present in my cpp class only test() is appearing in main.qml class

        raven-worxR 1 Reply Last reply
        0
        • S Sushma_MP

          there is no errors the thing is i am not able to access all methods which are present in my cpp class only test() is appearing in main.qml class

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Sushma_MP

          1. when it doesn't work, there is definitely an error/notice. Check the console.
          2. what do you mean with "appearing"? Do you mean QtCreator doesn't offer it in the auto-completion popup?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          S 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @Sushma_MP

            1. when it doesn't work, there is definitely an error/notice. Check the console.
            2. what do you mean with "appearing"? Do you mean QtCreator doesn't offer it in the auto-completion popup?
            S Offline
            S Offline
            Sushma_MP
            wrote on last edited by
            #5

            @raven-worx
            when it doesn't work, there is definitely an error/notice. Check the console.

            when we try to add new method try to access it in main.qml...like u said it is not appearing in auto-popup

            raven-worxR 1 Reply Last reply
            0
            • S Sushma_MP

              @raven-worx
              when it doesn't work, there is definitely an error/notice. Check the console.

              when we try to add new method try to access it in main.qml...like u said it is not appearing in auto-popup

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @Sushma_MP
              i don't know exactly when QtCreator updates it's completion model.
              But closing and reopening the project should trigger it.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              S 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @Sushma_MP
                i don't know exactly when QtCreator updates it's completion model.
                But closing and reopening the project should trigger it.

                S Offline
                S Offline
                Sushma_MP
                wrote on last edited by
                #7

                @raven-worx
                how is it possible then...everytime we can't close and reopen Qt creator rite...is it a bug...??

                raven-worxR 1 Reply Last reply
                0
                • sneubertS Offline
                  sneubertS Offline
                  sneubert
                  wrote on last edited by
                  #8

                  In Qt Creator did you try to call [Tools]->[QML/JS]->Reset Code Model, after you compiled your cpp class?

                  1 Reply Last reply
                  0
                  • S Sushma_MP

                    @raven-worx
                    how is it possible then...everytime we can't close and reopen Qt creator rite...is it a bug...??

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by raven-worx
                    #9

                    @Sushma_MP said in How to register and acess cpp class methods in QML:

                    how is it possible then...everytime we can't close and reopen Qt creator rite...is it a bug...??

                    i wasn't talking about restarting the whole QtCreator process, but only close and reopen the project.
                    The problem is, when is the right time to update the code-completion model?

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Sushma_MP
                      wrote on last edited by
                      #10

                      Hi ,

                      Actually i am facing another problem :
                      I created cpp class and registered in main.cpp and imported in main.qml
                      when i try to access that class in maim.qml i am getting as a invalid property .
                      my code is as fallow```
                      import QtQuick 2.4
                      import QtQuick.Controls 1.3
                      import QtQuick.Window 2.2
                      import QtQuick.Dialogs 1.2
                      import QtQuick 2.5
                      import QtGraphicalEffects 1.0
                      import com.songclass 1.0

                      ApplicationWindow {
                      flags: Qt.FramelessWindowHint

                      title: qsTr("Infotainment")
                      width: (Screen.width/2)+200
                      height: (Screen.height/2)
                      visible: true
                      modality: Qt.WindowModal
                      
                      selectsong{  
                      

                      here i get as invalid property with a red underlined for selectsong
                      }

                      MenuScreen{
                          id:menusrcn
                          visible: false
                      }
                      Settings{
                          id:settingScreen
                          visible: false
                      }
                      MusicVedio{
                          id:vedioScreen
                          visible: false
                      }
                      MediaPlay{
                          id:musicScreen
                          visible: false
                      }
                      Radio{
                          id:radioScreen
                          visible: false
                      }
                      
                      Rectangle{
                          id:main_rect
                          width: parent.width
                          height: parent.height
                          color: "#e9ebf8"
                          Image {
                              id: back_img
                              source: "qrc:/HomeScreen.png"
                              anchors.centerIn: parent
                          }
                      
                          Rectangle
                          {
                              id: welcome_rect
                              width: 250
                              height: 40
                              anchors.top: parent.top
                              anchors.topMargin: 100
                              anchors.horizontalCenter: parent.horizontalCenter
                              color: "transparent"
                              Text {
                                  id: welcome_text
                                  text: qsTr("L&T Car Infotainment")
                                  color:"#fefcfd"
                                  font.pixelSize: 36
                                  font.family: "Courier"
                                  font.bold: true
                                  font.italic: true
                                  anchors.centerIn: parent
                                  style: Text.Sunken
                                  styleColor: "#000000"
                              }
                          }
                      
                          Rectangle{
                              id: next_page_rect
                              width: parent.width/6
                              height: parent.height/12
                              color:"Transparent"
                              anchors.right: parent.right
                              anchors.bottom: parent.bottom
                              anchors.rightMargin: 15
                              anchors.bottomMargin: 8
                              Image {
                                  id: next_image
                                  source: "qrc:/Fwd_arrow.png"
                                  width: parent.width
                                  height: parent.height
                                  smooth: true
                                  fillMode: Image.PreserveAspectFit
                                  antialiasing: true
                                  MouseArea{
                                      id: next_page_rect_mousearea
                                      anchors.fill: parent
                                      onClicked: {
                                          menusrcn.visible= true
                                          main_rect.visible= false
                                      }
                                  }
                              }
                          }
                      }
                      

                      }

                      raven-worxR 1 Reply Last reply
                      0
                      • S Sushma_MP

                        Hi ,

                        Actually i am facing another problem :
                        I created cpp class and registered in main.cpp and imported in main.qml
                        when i try to access that class in maim.qml i am getting as a invalid property .
                        my code is as fallow```
                        import QtQuick 2.4
                        import QtQuick.Controls 1.3
                        import QtQuick.Window 2.2
                        import QtQuick.Dialogs 1.2
                        import QtQuick 2.5
                        import QtGraphicalEffects 1.0
                        import com.songclass 1.0

                        ApplicationWindow {
                        flags: Qt.FramelessWindowHint

                        title: qsTr("Infotainment")
                        width: (Screen.width/2)+200
                        height: (Screen.height/2)
                        visible: true
                        modality: Qt.WindowModal
                        
                        selectsong{  
                        

                        here i get as invalid property with a red underlined for selectsong
                        }

                        MenuScreen{
                            id:menusrcn
                            visible: false
                        }
                        Settings{
                            id:settingScreen
                            visible: false
                        }
                        MusicVedio{
                            id:vedioScreen
                            visible: false
                        }
                        MediaPlay{
                            id:musicScreen
                            visible: false
                        }
                        Radio{
                            id:radioScreen
                            visible: false
                        }
                        
                        Rectangle{
                            id:main_rect
                            width: parent.width
                            height: parent.height
                            color: "#e9ebf8"
                            Image {
                                id: back_img
                                source: "qrc:/HomeScreen.png"
                                anchors.centerIn: parent
                            }
                        
                            Rectangle
                            {
                                id: welcome_rect
                                width: 250
                                height: 40
                                anchors.top: parent.top
                                anchors.topMargin: 100
                                anchors.horizontalCenter: parent.horizontalCenter
                                color: "transparent"
                                Text {
                                    id: welcome_text
                                    text: qsTr("L&T Car Infotainment")
                                    color:"#fefcfd"
                                    font.pixelSize: 36
                                    font.family: "Courier"
                                    font.bold: true
                                    font.italic: true
                                    anchors.centerIn: parent
                                    style: Text.Sunken
                                    styleColor: "#000000"
                                }
                            }
                        
                            Rectangle{
                                id: next_page_rect
                                width: parent.width/6
                                height: parent.height/12
                                color:"Transparent"
                                anchors.right: parent.right
                                anchors.bottom: parent.bottom
                                anchors.rightMargin: 15
                                anchors.bottomMargin: 8
                                Image {
                                    id: next_image
                                    source: "qrc:/Fwd_arrow.png"
                                    width: parent.width
                                    height: parent.height
                                    smooth: true
                                    fillMode: Image.PreserveAspectFit
                                    antialiasing: true
                                    MouseArea{
                                        id: next_page_rect_mousearea
                                        anchors.fill: parent
                                        onClicked: {
                                            menusrcn.visible= true
                                            main_rect.visible= false
                                        }
                                    }
                                }
                            }
                        }
                        

                        }

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #11

                        @Sushma_MP
                        selectsong is not a valid property of ApplicationWindow type.
                        QML types must start with an Uppercase-Letter to distinguish them from properties as you can see in your error.

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Sushma_MP
                          wrote on last edited by
                          #12

                          ohh but selectsong is my cpp class name

                          raven-worxR 1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Sushma_MP
                            wrote on last edited by
                            #13

                            thank you soo much i got the solution by taking it as uppercase letter as you said...

                            1 Reply Last reply
                            0
                            • S Sushma_MP

                              ohh but selectsong is my cpp class name

                              raven-worxR Offline
                              raven-worxR Offline
                              raven-worx
                              Moderators
                              wrote on last edited by
                              #14

                              @Sushma_MP said in How to register and acess cpp class methods in QML:

                              ohh but selectsong is my cpp class name

                              you can also leave the class name, but register it with a upper case name should also work.

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              1 Reply Last reply
                              0

                              • Login

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