├── .gitignore ├── LICENSE ├── README.md ├── deployment.pri ├── main.cpp ├── main.qml ├── qml-test-6.pro ├── qml.qrc ├── testobject.cpp └── testobject.h /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.dll 11 | *.dylib 12 | 13 | # Qt-es 14 | 15 | /.qmake.cache 16 | /.qmake.stash 17 | *.pro.user 18 | *.pro.user.* 19 | *.moc 20 | moc_*.cpp 21 | qrc_*.cpp 22 | ui_*.h 23 | Makefile* 24 | *-build-* 25 | 26 | # QtCreator 27 | 28 | *.autosave 29 | 30 | #QtCtreator Qml 31 | *.qmlproject.user 32 | *.qmlproject.user.* 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Patrick Hanckmann 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cpp-qml-interaction 2 | =================== 3 | 4 | A minimal C++ QML interaction example 5 | 6 | It shows how to connect a QML GUI with a C++ backend using signals and slots. 7 | It is extensively documented. 8 | 9 | Good luck, I hope it is as usefull for you as it is for me! 10 | 11 | Patrick 12 | -------------------------------------------------------------------------------- /deployment.pri: -------------------------------------------------------------------------------- 1 | android-no-sdk { 2 | target.path = /data/user/qt 3 | export(target.path) 4 | INSTALLS += target 5 | } else:android { 6 | x86 { 7 | target.path = /libs/x86 8 | } else: armeabi-v7a { 9 | target.path = /libs/armeabi-v7a 10 | } else { 11 | target.path = /libs/armeabi 12 | } 13 | export(target.path) 14 | INSTALLS += target 15 | } else:unix { 16 | isEmpty(target.path) { 17 | qnx { 18 | target.path = /tmp/$${TARGET}/bin 19 | } else { 20 | target.path = /opt/$${TARGET}/bin 21 | } 22 | export(target.path) 23 | } 24 | INSTALLS += target 25 | } 26 | 27 | export(INSTALLS) 28 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "testobject.h" 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | QApplication app(argc, argv); 10 | 11 | QQmlApplicationEngine engine; 12 | TestObject to; 13 | 14 | // Load the QML and set the Context 15 | engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 16 | engine.rootContext()->setContextProperty("testObject",&to); 17 | 18 | return app.exec(); 19 | } 20 | -------------------------------------------------------------------------------- /main.qml: -------------------------------------------------------------------------------- 1 | import QtQuick 2.3 2 | import QtQuick.Controls 1.2 3 | 4 | ApplicationWindow { 5 | id: aw 6 | visible: true 7 | width: 640 8 | height: 480 9 | title: qsTr("Hello World") 10 | 11 | // Some interaction elements 12 | Text { 13 | id: someTxt 14 | text: qsTr("Hello World") 15 | anchors.centerIn: parent 16 | } 17 | Button { 18 | id: someBtn 19 | text: qsTr("someButton") 20 | anchors.centerIn: parent 21 | anchors.verticalCenterOffset: -40 22 | onClicked: { 23 | // Default button signal 24 | testObject.someSlot("fn-call") 25 | } 26 | } 27 | 28 | // Create connections with c++ 29 | Connections // Define actions for custom slots 30 | { 31 | id:cppConnection 32 | target:testObject 33 | ignoreUnknownSignals: true 34 | onSomeSignal: { 35 | // To access signal parameter, name the parameter 36 | someTxt.text = text 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /qml-test-6.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | 3 | QT += qml quick widgets 4 | 5 | SOURCES += main.cpp \ 6 | testobject.cpp 7 | 8 | RESOURCES += qml.qrc 9 | 10 | # Additional import path used to resolve QML modules in Qt Creator's code model 11 | QML_IMPORT_PATH = 12 | 13 | # Default rules for deployment. 14 | include(deployment.pri) 15 | 16 | HEADERS += \ 17 | testobject.h 18 | -------------------------------------------------------------------------------- /qml.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | main.qml 4 | 5 | 6 | -------------------------------------------------------------------------------- /testobject.cpp: -------------------------------------------------------------------------------- 1 | #include "testobject.h" 2 | #include 3 | 4 | #include 5 | 6 | TestObject::TestObject(QObject *parent) : 7 | QObject(parent) 8 | { 9 | } 10 | 11 | void TestObject::someSlot(const QString &text) 12 | { 13 | QString nText = text + ".cpp"; 14 | emit someSignal(nText); 15 | 16 | qDebug() << nText; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /testobject.h: -------------------------------------------------------------------------------- 1 | #ifndef TESTOBJECT_H 2 | #define TESTOBJECT_H 3 | 4 | #include 5 | 6 | class TestObject : public QObject 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit TestObject(QObject *parent = 0); 11 | 12 | signals: 13 | void someSignal(const QString &text); 14 | 15 | public slots: 16 | void someSlot(const QString &text); 17 | 18 | }; 19 | 20 | #endif // TESTOBJECT_H 21 | --------------------------------------------------------------------------------