How to register and access C++ class methods in QML
-
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() } } }
-
@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.
-
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
-
- when it doesn't work, there is definitely an error/notice. Check the console.
- what do you mean with "appearing"? Do you mean QtCreator doesn't offer it in the auto-completion popup?
-
@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
-
@Sushma_MP
i don't know exactly when QtCreator updates it's completion model.
But closing and reopening the project should trigger it. -
@raven-worx
how is it possible then...everytime we can't close and reopen Qt creator rite...is it a bug...?? -
In Qt Creator did you try to call [Tools]->[QML/JS]->Reset Code Model, after you compiled your cpp class?
-
@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? -
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.0ApplicationWindow {
flags: Qt.FramelessWindowHinttitle: 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 } } } } }
}
-
@Sushma_MP
selectsong
is not a valid property ofApplicationWindow
type.
QML types must start with an Uppercase-Letter to distinguish them from properties as you can see in your error. -
ohh but selectsong is my cpp class name
-
thank you soo much i got the solution by taking it as uppercase letter as you said...
-
@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.