How to connect a Qt Quick button click to a c++ method
binding, qml, qt
Solution
First, you need to export the SomeClass object to QtQuick (is that your third question?):
SomeClass sc;
viewer.rootContext()->setContextProperty(QStringLiteral("_someObject"), &sc);
This makes the sc object available in QtQuick, under the name "_someObject".
Then, in the QML use it like this:
Button {
....
onClicked: {
_someObject.buttonClicked(); //_someObject is the name used in setContextProperty.
}
}
This assumes the Button has a signal clicked() that's emitted on click/touch. Not knowing which Button component you use, I can't check that.
To pass an argument, just do
onClicked: {
_someObject.buttonClicked("Something");
}
Problem
I've just started to learn about Qt Mobile programming with Qt Quick 2.0 and I've been on google the whole day and its driving me crazy so here goes. I have got just your standard qt quick mobile app that qt makes for you. Here is all the files. (I'm brand new to this so these might be noob qu's, sorry) Ok so here is my list of I Don't Knows: - how on earth do I connect this class method to the button click in the qml file. Would it be possible to connect the same click and pass params through the click from the qml to the method and if so how would I do it? and also I was wondering if I could link the class to the qt quick like a regular QWidget class for instance: ``` int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); SomeClass *sc = new SomeClass(); QtQuick2ApplicationViewer viewer; // this I guess would be more like the Widget method but this i really dont know // about, just a question I'm throwing out there viewer. (Link qt quick qml to object "sc"(Containing all my processes)) viewer.setMainQmlFile(QStringLiteral("qml/Final2/main.qml")); viewer.showExpanded(); return app.exec(); } ``` The main(.)cpp file ``` #include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/Final2/main.qml")); viewer.showExpanded(); return app.exec(); } ``` This is just the button element from my main.qml ``` Button { id: btn y: 443 height: 39 text: "Click Me" anchors.bottom: parent.bottom anchors.bottomMargin: 8 anchors.right: parent.right anchors.rightMargin: 25 anchors.left: parent.left anchors.leftMargin: 15 } ``` This is just some random class header : ``` #ifndef SOMECLASS_H #define SOMECLASS_H #include <QObject> #include <QDebug>> class SomeClass : public QObject { Q_OBJECT public: explicit SomeClass(QObject *parent = 0); signals: public slots: void buttonClicked(); void buttonClicked(QString &in); }; #endif // SOMECLASS_H ``` Of course the cpp file : ``` #include "someclass.h" SomeClass::SomeClass(QObject *parent) : QObject(parent) { } void SomeClass::buttonClicked() { // do something } void SomeClass::buttonClicked(QString &in) { qDebug() << "Your string was : " << in; } ``` I really appreciate all the help. Thank you.