Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Qt Multiplatform development

263 views

Published on

Basic concepts in multiplatform Android, iOS, MacOS development using C++ Qt Framework and QML.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Qt Multiplatform development

  1. 1. start Qt for beginners Multiplatform development with QtFramework Sergiy Shevchenko eTuitus S.r.l 2016 Part 2
  2. 2. Table of Contents 01 Qml/C++ communication Basic signal/slot concepts of qml/c++ communication 02 Qt for mobile Cross-platform mobile development 03 Connecting C++ to native How to interface with native Java and Objective C environments next2
  3. 3. next3 QML/Qt communication
  4. 4. next4 QML/Qt communication • Gui creates an event (click on button) • QML (via JS) invokes business logic in Qt • Business logic does something and returns a result
  5. 5. next5 QML/Qt communication Prepare object to expose (from C++ to QML) //AbstractVibrator.h #include <QObject> class AbstractVibrator : public QObject { Q_OBJECT public: explicit AbstractVibrator(QObject *parent = 0); Q_INVOKABLE virtual bool vibrate() = 0; }; Extends QObject Q_INVOKABLE macro
  6. 6. next6 QML/Qt communication Expose AbstractVibrator to QML //main.cpp #include <QQmlApplicationEngine> #include <QQmlContext> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; qmlRegisterType<AbstractVibrator>(); engine.rootContext()-> setContextProperty("facadeVibrator", NativeAbstraction::getVibrateImpl()); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); } Include DOM manipulation headers Register new QML type Set name and pass a pointer
  7. 7. next7 QML/Qt communication Access to facade from QML Window { //... MainForm { //... vibrate.onClicked: { if(facadeVibrator.vibrate()){ console.log("Vibrate success"); }else{ console.log("Vibration failed"); } } } Just invoke function by name
  8. 8. next8 QML/Qt communication Datatypes JavaScript (QML) is dynamic, untyped, and interpreted programming language. On other side C++ is compiled and statically typed language. How do they deal together? • QVariant (QString, int ecc..) • Registering type with qRegisterMetaType and qmlRegisterType
  9. 9. next9 QML/Qt communication Datatypes - pointers Do not use pointers to C++ objects in QML! If you have to, make a copy of object and return a pointer to a copy Why? 
 JavaScript has a GC so it will deallocate pointed object at most inconvenient time
  10. 10. next10 QML/Qt communication Datatypes - pointers SigningLibrary.cpp SigningFacade.cpp
  11. 11. next11 QML/Qt communication Sync/Async Invoking C++ code from QML means that C++ will be executed in the same thread (GUI) Solution
 use Facade pattern
  12. 12. next12 QML/Qt communication Facade design pattern QML C++
  13. 13. next13 QML/Qt communication Async facade Event starts here Create new Thread
 and invoke getPrime Invoke C++ obtain a return Once result is ready
 emit signal
  14. 14. next14 QML/Qt communication Async Facade MainForm.ui.qml
  15. 15. next15 QML/Qt communication Async Facade MainForm.qml
  16. 16. next16 QML/Qt communication Async Facade main.cpp PrimeFacade.h PrimeFacade.cpp PrimeGenerator.h
  17. 17. next17 QML/Qt communication LIVE
  18. 18. next18 Cross-platform mobile development Integration with native APIs
  19. 19. next19 What do we need? •Qt >= 5.5 •Android SDK Tools e Android Studio •Android NDK •JDK v7 or major •XCode 7
  20. 20. next20 Generic application Packaging • .APK • Gradle or ANT • .IPA • XCode export tool
  21. 21. next21 QtCreator config
  22. 22. next22 Simple project Once pressed "Vibrate" button device must vibrate using integrated vibrator Problem? Each platform realises vibration API differently and in native code! QT -> NATIVE
  23. 23. next23 Things to do • Lets create an empty project or use existing one (will be provided) • Make gui with simple call to facade • Create platform independent abstraction for vibration service • Write function calling Android Vibration API in java • Write function calling iOS Vibration API in Objective C • Implement bridges and trampoline for Android and iOS respectively • Be happy!
  24. 24. next24 Project setup • Modular structure • /platform/ contains all native code • android project can be opened with Android Studio • Once compiled iOS project can be edited in XCode
  25. 25. next25 Vibration abstractions <<use>> <<use>> <<use>> <<use>>
  26. 26. next26 JNI The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call and be called by native applications or libraries written in other languages such as C, C++ and assembly.
  27. 27. next27 Vibration abstractions AbstractVibrator.h AndroidVibrator.hSpecific implementation
  28. 28. next28 Android Native Impl. AndroidVibrator.cpp EtMobileActivity.java
  29. 29. next29 IOS Native implementation VibratorTrampoline.h IOSVibrator.cpp VibratorTrampoline.mm
  30. 30. next30 Orchestrating classes NativeAbstraction.cpp NativeAbstraction.h main.cpp
  31. 31. next31 Orchestrating files core.pro
  32. 32. next32 Handling device events • For iOS things are simple: just include • Android needs JNI exported functions NATIVE -> QT
  33. 33. next33 Simple case: iOS AppDelegate.mm just include *.h use Qt/C++ code
  34. 34. next34 Complex case: Android •Export C functions using JNIEXPORT •Create "bridge" class with native methods in Java •Ensure Qt is running and invoke methods Things to do
  35. 35. next35 Complex case: Android Export C functions src/it/infocert/mobile/dike/JNIOpener.java
  36. 36. next36 Complex case: Android Thread lifecycle 1.Call a Java method from C/C++ Qt thread. The Java method will be executed in Qt thread, so we we need a way to access Android APIs in Android UI thread. 2.Java method uses Activity.runOnUiThread to post a runnable on Android UI thread. This runnable will be executed by the Android event loop on Android UI thread. 3.The runnable accesses the Android APIs from Android UI thread. 4.Call a C/C++ function from Android UI thread. 5.Using QMetaObject::invokeMethod to post a method call on Qt event loop. 6.Qt event loop will execute that function on Qt thread.
  37. 37. next37 Complex case: Android Thread lifecycle NO main() Start App
  38. 38. next38 Complex case: Android Java -> QT
  39. 39. next39 Complex case: Android Java -> QT
  40. 40. next40 Complex case: Android Check Qt Startup Start App
  41. 41. Complex case: Android Check Qt Startup QtStartMonitor.h next
  42. 42. next42 Complex case: Android Check Qt Startup NativeBridge.java AndroidBridge.cpp main.cpp Somewhere in java...
  43. 43. close Thank You Presenter: Sergiy Shevchenko eTuitus S.r.l 2016

×