Calling Qt-Gui from native iOS Gui & Vice versa
-
I currently have a native iOS GUI and a Qt-GUI. I'm trying to switch from one to another.
To be clear: When i click on a button on the native GUI i want the Qt-GUI to show up and vice versa.
I already found out which libraries i have to add to be able to use the Qt-Stuff. I created a
QApplication
in theAppDelegate.mm
file:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions { // receive int argc, and char** argv for the QApplication. _qApp = new QApplication(_argc, _argv); }
Furthermore my Qt application looks (at the moment) like this:
void createQtGUI() { QPushButton* btn = new QPushButton("Some Button"); QLabel* lbl = new QLabel("QTGui"); QVBoxLayout* layout = new QVBoxLayout(); layout->addWidget(lbl); layout->addWidget(btn); QWidget* window = new QWidget(); window->setLayout(layout); window->show(); }
I'm calling the
createQtGUI
method in myViewController.mm
when pressing a button in the native iOS GUI. The code runs without throwing any error, but:The Qt-GUI is not shown. The application still shows the native gui without switching to the Qt-GUI.
Has anybody got any idea how to fix that?
-
I've finally found out what was missing:
The Qt-Objects provide a method called
winId()
. This method returns aWId
which actually is (on iOS) anUIView*
.You have to add that
UIView*
as Subview to your main view.
In order to achieve that I changed my
createQtGUI
method as follows:WId createQtGUI() { ... // nothing changed here (only at the end) window->show(); return window->winId(); }
And In my
ViewController.mm
(where I call my method):- (IBAction)ButtonClicked:(id)sender { UIView* newView = (__bridge UIView*)reinterpret_cast<void*>(createQtGUI()); [self.view addSubview:newView]; }
Note: The double cast
(__bridge UIView*)reinterpret_cast<void*>(...)
is necessary because you can't just cast fromWId
toUIView*
in Objective-C++. -
Hi all.
I'd rather raise this old topic, than create an identical one.
I'm trying to achieve exactly the same functionality using iOS12 (since yesterday) and Qt 5.10.1 (more recent versions are to be tested yet). It does not work. No updates, no events are coming to my Qt window, and it's not being drawn. Does anyone has a working solution?I have gone through following resources without success:
https://github.com/msorvig/qt-and-swift
https://forum.qt.io/topic/65979/calling-qt-gui-from-native-ios-gui-vice-versa
https://alediaferia.com/2014/12/18/a-native-ios-app-with-a-qt-third-party-library/
https://github.com/richardmg/HybridQtIosApp/tree/master/HybridQtAppp.s. I will post sample code a bit later.
Thanks in advance!
/Sergey
-
@eliseev The basic schema is depending on where are you going to build application release:
- In QT Creator and add iOS/MacOS native code to QT
- In XCode and QT code into iOS/MacOS
In few projects before I've been trying a lot of options but stopped onto building static *.a library independent for UI and Backend and mix it in project.
The schema for first point:
- Create project in QT for UI, doesn't matter widget or quick.
- Create in XCode Objective-C (!!! NOT Swift) static library project for backend. Through this schema you might even add Swift code via cross-header file into Objective-C library. You might be using Objective-C directly in QT Creator but it's not a good way because of many points, https://www.raywenderlich.com/2658-creating-a-static-library-in-ios-tutorial
- Build static library *.a file in XCode and add IT into QT Project in *.pro file
- Write wrapper via using Objective-CPP *.mm in QT Project. Something like this
- Build QT Project within iOS/MacOS native static library
The schema for second point not well researched by me but in principle it's similar. I've got troubles in building QML like static library and then using it in XCode. For using widgets - works fine, but for mobile, in following this forum not a good way to use widgets.
All of it my own way that is OK for me. Maybe there are another ...
Beside all of it be ready for (all of it from my own experience):
- Data types conversion troubles. Sometimes it will rise you upon huge complexity. Briefly - you will transfer data from Objective-C to Objective-CPP to CPP/QT and only then into JS/QML. Read carefully the conversion pairs for every step. The data conversion between CPP and JS/QML not so simple how it might be looking for the first glance and you will add to it complexity of adding native to CPP
- The Objective-C and Objective-CPP (especially) manuals became very rare option because of wiping out it in exchanging to Swift. Sometimes I am building something on Swift for understanding of how it works, after it rebuilding it on Objective-C and only after building it like add-on for QT project. I am doing this way only because of absence manuals for Objective-C, especially for something new.
- QT cross-platform from begin and it means you might to do it for many platforms from begin. I would strongly recommend to use QT for whole application and native for adding something that not implemented in QT. If you will do something native in background it will be working only on native.
- The philosophy of QT extremely different to native. Native is trying to make your life easy. QT DO NOT care about your life in a way of native, QT care about your ability to develop something unique. QT - very powerfull framework for developing solutions but native - solution itself.
- After writing on one language Objective-C (or Swift) you will be required be fluent in CPP, JS and QML at once. In your case it will be 2 more Objective-C and Objective-CPP. It might be very curious at time of start of developing. For me it costs almost 2 years.
I am using QT since 2012-2013 and have been doing the same - first UI on QT but backend on native. For now - only QT. I am developing for mobile, mostly for Apple devices.
-
@bogong For me it's normal develop qt (also for mobile application), but in this time I must develop a strange case, I must use a native controller (that it can't be emulate in qt/qml) and from this I must start the qml GUI. It's all ok, I see the qml elements but I can't catch the user interaction, not press button, nothing. I started from Qt project where I added native object-c files, AppDelegate, other native controller, etc.