Top Banner
start Qt for beginners Multiplatform development with QtFramework Sergiy Shevchenko eTuitus S.r.l 2016 Part 2
43

Qt Multiplatform development

Jan 22, 2018

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Qt Multiplatform development

start

Qt for beginners

Multiplatform development

with QtFramework

Sergiy Shevchenko

eTuitus S.r.l 2016

Part 2

Page 2: Qt Multiplatform development

Table of

Contents

01 Qml/C++ communicationBasic signal/slot concepts of qml/c++ communication

02 Qt for mobileCross-platform mobile development

03 Connecting C++ to nativeHow to interface with native Java and Objective C environments

next2

Page 3: Qt Multiplatform development

next3

QML/Qt communication

Page 4: Qt Multiplatform development

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

Page 5: Qt Multiplatform development

next5

QML/Qt communicationPrepare object to expose (from C++ to QML)

//AbstractVibrator.h#include <QObject>

class AbstractVibrator : public QObject{ Q_OBJECTpublic: explicit AbstractVibrator(QObject *parent = 0);

Q_INVOKABLE virtual bool vibrate() = 0;};

Extends QObject

Q_INVOKABLE macro

Page 6: Qt Multiplatform development

next6

QML/Qt communicationExpose 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

Page 7: Qt Multiplatform development

next7

QML/Qt communicationAccess 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

Page 8: Qt Multiplatform development

next8

QML/Qt communicationDatatypes

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

Page 9: Qt Multiplatform development

next9

QML/Qt communicationDatatypes - 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

Page 10: Qt Multiplatform development

next10

QML/Qt communicationDatatypes - pointers

SigningLibrary.cpp

SigningFacade.cpp

Page 11: Qt Multiplatform development

next11

QML/Qt communicationSync/Async

Invoking C++ code from QML means that C++ will be executed in the same thread (GUI)

Solution use Facade pattern

Page 12: Qt Multiplatform development

next12

QML/Qt communicationFacade design pattern

QML

C++

Page 13: Qt Multiplatform development

next13

QML/Qt communicationAsync facade

Event starts here

Create new Thread and invoke getPrime

Invoke C++obtain a return

Once result is ready emit signal

Page 14: Qt Multiplatform development

next14

QML/Qt communicationAsync Facade

MainForm.ui.qml

Page 15: Qt Multiplatform development

next15

QML/Qt communicationAsync Facade MainForm.qml

Page 16: Qt Multiplatform development

next16

QML/Qt communicationAsync Facade

main.cpp

PrimeFacade.h PrimeFacade.cpp

PrimeGenerator.h

Page 17: Qt Multiplatform development

next17

QML/Qt communication

LIVE

Page 18: Qt Multiplatform development

next18

Cross-platform mobile development

Integration with native APIs

Page 19: Qt Multiplatform development

next19

What do we need?

•Qt >= 5.5•Android SDK Tools e Android Studio•Android NDK•JDK v7 or major•XCode 7

Page 20: Qt Multiplatform development

next20

Generic application

Packaging• .APK• Gradle or ANT• .IPA• XCode export

tool

Page 21: Qt Multiplatform development

next21

QtCreator config

Page 22: Qt Multiplatform development

next22

Simple projectOnce pressed "Vibrate" button device must

vibrate using integrated vibrator

Problem?Each platform realises vibration API differently and in native code!

QT -> NATIVE

Page 23: Qt Multiplatform development

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!

Page 24: Qt Multiplatform development

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

Page 25: Qt Multiplatform development

next25

Vibration abstractions

<<use>>

<<use>>

<<use>>

<<use>>

Page 26: Qt Multiplatform development

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.

Page 27: Qt Multiplatform development

next27

Vibration abstractions

AbstractVibrator.h

AndroidVibrator.hSpecificimplementation

Page 28: Qt Multiplatform development

next28

Android Native Impl.

AndroidVibrator.cpp

EtMobileActivity.java

Page 29: Qt Multiplatform development

next29

IOS Native implementation

VibratorTrampoline.h

IOSVibrator.cpp

VibratorTrampoline.mm

Page 30: Qt Multiplatform development

next30

Orchestrating classesNativeAbstraction.cpp

NativeAbstraction.h

main.cpp

Page 31: Qt Multiplatform development

next31

Orchestrating filescore.pro

Page 32: Qt Multiplatform development

next32

Handling device events

• For iOS things are simple: just include l• Android needs JNI exported functions

NATIVE -> QT

Page 33: Qt Multiplatform development

next33

Simple case: iOS

AppDelegate.mm

just include *.h

use Qt/C++ code

Page 34: Qt Multiplatform development

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

Page 35: Qt Multiplatform development

next35

Complex case: AndroidExport C functions

src/it/infocert/mobile/dike/JNIOpener.java

Page 36: Qt Multiplatform development

next36

Complex case: AndroidThread 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.

Page 37: Qt Multiplatform development

next37

Complex case: AndroidThread lifecycle

NO

main()

Start App

Page 38: Qt Multiplatform development

next38

Complex case: AndroidJava -> QT

Page 39: Qt Multiplatform development

next39

Complex case: AndroidJava -> QT

Page 40: Qt Multiplatform development

next40

Complex case: AndroidCheck Qt Startup

Start App

Page 41: Qt Multiplatform development

Complex case: AndroidCheck Qt Startup

QtStartMonitor.h

next

Page 42: Qt Multiplatform development

next42

Complex case: AndroidCheck Qt Startup NativeBridge.java

AndroidBridge.cpp

main.cpp

Somewhere in java...

Page 43: Qt Multiplatform development

close

Thank You

Presenter: Sergiy Shevchenko

eTuitus S.r.l 2016