├── qml.qrc ├── uppercase.cpp ├── uppercase.h ├── qml2-to-cpp-and-back.pro ├── README.md ├── main.cpp ├── main.qml ├── deployment.pri └── qml2-to-cpp-and-back.pro.user /qml.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | main.qml 4 | 5 | 6 | -------------------------------------------------------------------------------- /uppercase.cpp: -------------------------------------------------------------------------------- 1 | #include "uppercase.h" 2 | 3 | Uppercase::Uppercase(QObject *parent) : 4 | QObject(parent) 5 | { 6 | } 7 | 8 | QString Uppercase::uppercase(const QString &in) 9 | { 10 | qDebug() << "c++: " << in; 11 | return in.toUpper(); 12 | } 13 | -------------------------------------------------------------------------------- /uppercase.h: -------------------------------------------------------------------------------- 1 | #ifndef UPPERCASE_H 2 | #define UPPERCASE_H 3 | 4 | #include 5 | #include 6 | 7 | class Uppercase : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit Uppercase(QObject *parent = 0); 12 | 13 | signals: 14 | 15 | public slots: 16 | QString uppercase(const QString& in); 17 | 18 | }; 19 | 20 | #endif // UPPERCASE_H 21 | -------------------------------------------------------------------------------- /qml2-to-cpp-and-back.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | 3 | QT += qml quick 4 | 5 | SOURCES += main.cpp \ 6 | uppercase.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 | uppercase.h 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | qt-qml2-to-cpp-and-back 2 | ======================= 3 | 4 | Simple example of integrating QML2 and C++. 5 | 6 | See the blog post for a walkthough: http://andrew-jones.com/blog/qml2-to-c-and-back-again/ 7 | 8 | A slightly more complicated way to do this, using [signals and slots](http://qt-project.org/doc/qt-5/signalsandslots.html), can be found in [andrewrjones/qml2-to-cpp-and-back-signals](/andrewrjones/qml2-to-cpp-and-back-signals). 9 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "uppercase.h" 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | QGuiApplication app(argc, argv); 10 | 11 | QQmlApplicationEngine engine; 12 | engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 13 | 14 | Uppercase uppercase; 15 | 16 | engine.rootContext()->setContextProperty("_uppercase", &uppercase); 17 | 18 | return app.exec(); 19 | } 20 | -------------------------------------------------------------------------------- /main.qml: -------------------------------------------------------------------------------- 1 | import QtQuick 2.3 2 | import QtQuick.Window 2.2 3 | import QtQuick.Controls 1.2 4 | 5 | Window { 6 | visible: true 7 | width: 360 8 | height: 360 9 | 10 | MouseArea { 11 | anchors.fill: parent 12 | onClicked: { 13 | Qt.quit(); 14 | } 15 | } 16 | 17 | TextField { 18 | id: textField1 19 | x: 31 20 | y: 169 21 | placeholderText: qsTr("Enter some text...") 22 | } 23 | 24 | Button { 25 | x: 193 26 | y: 167 27 | text: qsTr("Uppercase me!") 28 | onClicked: 29 | textField1.text = _uppercase.uppercase(textField1.text) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /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 | !host_build:QMAKE_MAC_SDK = macosx10.9 27 | 28 | export(INSTALLS) 29 | -------------------------------------------------------------------------------- /qml2-to-cpp-and-back.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {4fc68aad-7d47-459d-b466-e73ff8e4fbe4} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | 0 45 | 8 46 | true 47 | 1 48 | true 49 | true 50 | true 51 | false 52 | 53 | 54 | 55 | ProjectExplorer.Project.PluginSettings 56 | 57 | 58 | 59 | ProjectExplorer.Project.Target.0 60 | 61 | Desktop Qt 5.3 clang 64bit 62 | Desktop Qt 5.3 clang 64bit 63 | qt.53.clang_64_kit 64 | 0 65 | 0 66 | 0 67 | 68 | /Users/ajones/dev/qt/build-qml2-to-cpp-and-back-Desktop_Qt_5_3_clang_64bit-Debug 69 | 70 | 71 | true 72 | qmake 73 | 74 | QtProjectManager.QMakeBuildStep 75 | false 76 | true 77 | 78 | false 79 | 80 | 81 | true 82 | Make 83 | 84 | Qt4ProjectManager.MakeStep 85 | 86 | -w 87 | -r 88 | 89 | false 90 | 91 | 92 | 93 | 2 94 | Build 95 | 96 | ProjectExplorer.BuildSteps.Build 97 | 98 | 99 | 100 | true 101 | Make 102 | 103 | Qt4ProjectManager.MakeStep 104 | 105 | -w 106 | -r 107 | 108 | true 109 | clean 110 | 111 | 112 | 1 113 | Clean 114 | 115 | ProjectExplorer.BuildSteps.Clean 116 | 117 | 2 118 | false 119 | 120 | Debug 121 | 122 | Qt4ProjectManager.Qt4BuildConfiguration 123 | 2 124 | true 125 | 126 | 127 | /Users/ajones/dev/qt/build-qml2-to-cpp-and-back-Desktop_Qt_5_3_clang_64bit-Release 128 | 129 | 130 | true 131 | qmake 132 | 133 | QtProjectManager.QMakeBuildStep 134 | false 135 | true 136 | 137 | false 138 | 139 | 140 | true 141 | Make 142 | 143 | Qt4ProjectManager.MakeStep 144 | 145 | -w 146 | -r 147 | 148 | false 149 | 150 | 151 | 152 | 2 153 | Build 154 | 155 | ProjectExplorer.BuildSteps.Build 156 | 157 | 158 | 159 | true 160 | Make 161 | 162 | Qt4ProjectManager.MakeStep 163 | 164 | -w 165 | -r 166 | 167 | true 168 | clean 169 | 170 | 171 | 1 172 | Clean 173 | 174 | ProjectExplorer.BuildSteps.Clean 175 | 176 | 2 177 | false 178 | 179 | Release 180 | 181 | Qt4ProjectManager.Qt4BuildConfiguration 182 | 0 183 | true 184 | 185 | 2 186 | 187 | 188 | 0 189 | Deploy 190 | 191 | ProjectExplorer.BuildSteps.Deploy 192 | 193 | 1 194 | Deploy locally 195 | 196 | ProjectExplorer.DefaultDeployConfiguration 197 | 198 | 1 199 | 200 | 201 | 202 | false 203 | false 204 | false 205 | false 206 | true 207 | 0.01 208 | 10 209 | true 210 | 1 211 | 25 212 | 213 | 1 214 | true 215 | false 216 | true 217 | valgrind 218 | 219 | 0 220 | 1 221 | 2 222 | 3 223 | 4 224 | 5 225 | 6 226 | 7 227 | 8 228 | 9 229 | 10 230 | 11 231 | 12 232 | 13 233 | 14 234 | 235 | -1 236 | 237 | 238 | 239 | false 240 | %{buildDir} 241 | Custom Executable 242 | 243 | ProjectExplorer.CustomExecutableRunConfiguration 244 | 3768 245 | false 246 | true 247 | false 248 | false 249 | true 250 | 251 | 1 252 | 253 | 254 | 255 | ProjectExplorer.Project.TargetCount 256 | 1 257 | 258 | 259 | ProjectExplorer.Project.Updater.FileVersion 260 | 16 261 | 262 | 263 | Version 264 | 16 265 | 266 | 267 | --------------------------------------------------------------------------------