Jutting Bytes

Digressions of a research engineer

Qt on iOS Examples

| Comments

The Qt project has recently published a post on recent advances in writing an iOS platform plugin. So far, many features are working just as expected, and I must say, these features cover the needs of many many applications we have written so far. So yes, we can move forward porting our applications to iOS.

The first part of this post shows how to set up a development environment, note it merely summarises information given on the post, plus some additional hints to help you run the examples provided in the second part of this post.

The second part introduces a repository that demonstrates the behaviour of some relevant features of Qt, both in desktop, simulator and device configurations. Note they do no constitute a full demo application (check this one if you are looking for one), but a set of barely enhanced feature based hello world programs.

As usual, repository on GitHub.

Setting up a Qt development environment for iOS

An iOS app development can be achieved either in simulator or device mode. The first one is very convenient for rapid prototyping as it does not require a device to be plugged to your computer, or transferring a compiled app to the device. The second one however is mandatory to get a real user experience of your development. This two modes, referred to a configurations differ in many ways, the most significant being the build architecture: a simulator configuration builds for your computer architecture, e.g. i386, whereas a device configuration builds for your device architecture, e.g. armv7.

The two procedures are described for convenience, as the second one only adds a configuration switch. Note that distinct qt repository checkouts are proposed to make this guide easy to follow, but feel free to build any way that suits your habits.

Building in simulator mode

$ git clone git://gitorious.org/qt/qt5.git qt-ios-simulator
$ cd qt-ios-simulator/
$ perl init-repository
$ cd qtbase
$ git checkout dev
$ ./configure -xplatform unsupported/macx-ios-clang -developer-build -opensource -confirm-license -nomake examples -nomake tests -release -sdk iphonesimulator
$ make -j24
$ cd ../qtscript
$ ../qtbase/bin/qmake
$ make -j24
$ cd ../qtquick1
$ ../qtbase/bin/qmake
$ make -j24

Optionally, symlink the device configuration qmake binary, in whatever path you use to look qmake into, to qmake-ios-simulator.

~/Development/qt/5.0.0/clang_64/bin$ ln -s ~/Development/qt-ios-simulator/qtbase/bin/qmake qmake-ios-simulator

Building in device mode

$ git clone git://gitorious.org/qt/qt5.git qt-ios-device
$ cd qt-ios-device/
$ perl init-repository
$ cd qtbase
$ git checkout dev
$ ./configure -xplatform unsupported/macx-ios-clang -developer-build -opensource -confirm-license -nomake examples -nomake tests -release
$ make -j24
$ cd ../qtscript
$ ../qtbase/bin/qmake
$ make -j24
$ cd ../qtquick1
$ ../qtbase/bin/qmake
$ make -j24

Optionally, symlink the device configuration qmake binary, in whatever path you use to look qmake into, to qmake-ios-device.

~/Development/qt/5.0.0/clang_64/bin$ ln -s ~/Development/qt-ios-device/qtbase/bin/qmake qmake-ios-device

And there you go. From now on, your development environment is set up, including base libraries and headers: QtCore, QtConcurrent, QtNetwork, QtGui, QtNetwork, QtWidgets etc., but also QtScript, QtScriptTools and QtDeclarative.

Examples for Qt on iOS

qt-ios-examples project allows to keep track of functional features the iOS Qt platform plugin that I may need to port my applications to the iOS platform.

Building in desktop mode

$ cd /path/to/qt-ios-examples
$ mkdir build-desktop
$ cd build-desktop
$ cmake .. -G Ninja
$ ninja

Building in simulator mode

$ cd /path/to/qt-ios-examples
$ mkdir build-simulator
$ cd build-simulator
$ cmake .. -G Ninja -DIOS-SIMULATOR=ON
$ open qt-ios-example-XXX-YYY.xcodeproj
$ open qt-ios-example-XXX-ZZZ.xcodeproj

Building in device mode

$ cd /path/to/qt-ios-examples
$ mkdir build-device
$ cd build-device
$ cmake .. -G Ninja -DIOS-DEVICE=ON
$ open qt-ios-example-XXX-YYY.xcodeproj
$ open qt-ios-example-XXX-ZZZ.xcodeproj

Notes

qt-ios-examples introduces very few useful additions when it comes to write cross architecture/platform Qt based applications. For example, the root CMakeLists provides two options, as seen in the above build instructions.

1
2
option(IOS-DEVICE    "iOS device    build" OFF)
option(IOS-SIMULATOR "iOS simulator build" OFF)

These options, yet part of the project build configuration step, have some impact the code writing, but note that this impact is completely optional and only relevant in the context of this project. Here is how these options are handled.

1
2
3
4
5
6
7
8
9
if(IOS-DEVICE)
  add_definitions("-DQ_OS_IOS")
  add_definitions("-DQ_OS_IOS_DEVICE")
endif(IOS-DEVICE)

if(IOS-SIMULATOR)
  add_definitions("-DQ_OS_IOS")
  add_definitions("-DQ_OS_IOS_SIMULATOR")
endif(IOS-SIMULATOR)

Here is an example of using these options in the actual examples code, in this case because Qt-5.1.0 doesn’t handles the the WA_AcceptTouchEvents window attribute so far.

1
2
3
#if defined(Q_OS_MAC) && !defined(Q_OS_IOS)
    [reinterpret_cast<NSView *>(this->winId()) setAcceptsTouchEvents: YES];
#endif

Caveats

qt-ios-examples uses CMake as a build system, to match the one of the various existing applications I want to port to iOS. This is why cmake toolchain files (useful to cross compile, or more generally, useful for setting up different build configurations) have been included in this project. They are however not functional for the time being. If applications build fine, they do not run in the simulator, they have to be updated to match mkspecs/unsupported/macx-ios-clang/qmake.conf, which includes mkspecs/common/ios/qmake.conf and other.


Comments