Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

How to run a C++ function when QML button is clicked? Using QQmlApplicationEngine

$
0
0
It works for me. main.cpp int main(int argc, char *argv[]) {     QApplication app(argc, argv);       QQmlApplicationEngine engine;     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));     QQmlContext *item = engine.rootContext();     MyClass myClass;     item->setContextProperty("_myClass", &myClass);       return app.exec(); } MyClass.h class MyClass : public QObject {         Q_OBJECT     public:         //explicit MyClass(QObject *parent = 0) : QObject(parent) {}         MyClass() {}       signals:       public slots:          void buttonClicked(QString in)          {              qDebug() << "buttonClicked" << in;          }   }; main.qml import QtQuick 2.2 import QtQuick.Controls 1.1   ApplicationWindow {     visible: true     width: 640     height: 480     title: qsTr("Hello World")       menuBar: MenuBar {         Menu {             title: qsTr("File")             MenuItem {                 text: qsTr("Exit")                 onTriggered: Qt.quit();             }         }     }         Rectangle {         id: rectangle1         width: 100         height: 100         color: "red"         border.color: "black"         border.width: 5         radius: 10     }       MouseArea {         id: mouseArea1         anchors.fill: parent         hoverEnabled: true;         onEntered: { rectangle1.border.width = 2 }         onExited: { rectangle1.border.width = 1 }         onClicked: _myClass.buttonClicked("Worked?")     } }

Viewing all articles
Browse latest Browse all 4972

Trending Articles