Adding a QWindow to UIKit application on iOS
-
Hi all,
This is essentially a continuation of this topic:
https://forum.qt.io/topic/65979/calling-qt-gui-from-native-ios-gui-vice-versaI have a requirement to embed Qt content to an iOS application written in Swift.
For testing purposes I am using Objective-C at first.For the example below I am using RasterWindow class from here: https://github.com/msorvig/qt-and-swift/tree/master/QtApplication
I have created a single-view iOS application using XCode. Then converted it Obj-C++ by changing extensions form ".m" to ".mm". Added Qt library and header paths to the project. Added RasterWindow class. And now I am trying to instantiate and display it like this:
#include <QApplication> #include "rasterwindow.h" @interface ViewController () @end @implementation ViewController RasterWindow* _rwnd; - (void)viewDidLoad { [super viewDidLoad]; static int argc = 0; static char* argv = nullptr; QGuiApplication* qtAppInstance = new QGuiApplication(argc, &argv); _rwnd = new RasterWindow(); _rwnd->show(); self.view = (__bridge UIView*)reinterpret_cast<void*>(_rwnd->winId()); // self.view.contentMode = UIViewContentModeScaleToFill; // UIView* qtView = (__bridge UIView*)reinterpret_cast<void*>(_rwnd->winId()); // [self.view addSubview:qtView]; NSLog(@"view did load %@", self.view); // Do any additional setup after loading the view, typically from a nib. }
Execution produces following log:
virtual void RasterWindow::showEvent(QShowEvent *) QRect(0,0 0x0) 2018-09-21 06:43:08.165951+0200 QtInObjCiOS[47805:764033] view did load <QUIView: 0x7ffe72409b90; frame = (0 0; 0 0); layer = <CAEAGLLayer: 0x6000027ffb80>> -[ViewController viewDidAppear:] QRasterWindow(0x6000003b6a60) QRect(0,0 834x1112)
Visible result is a black screen, the window is not being drawn. If I change from assigning Qt window to self.view to adding it as a subview, then behaviour changes so that RasterWindow instance will have (0,0) size, and I couldn't force it to take size.
Note, same code works for Mac Cocoa using NSView. As does this simple app
#include "rasterwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); RasterWindow w; w.show(); return a.exec(); }
What am I missing? How do I correctly add a QWindow to a UIViewController?
Thank you in advance!
p.s. Complete project: https://www.dropbox.com/s/mz6ty3922u6vcdp/QtInObjCiOS.zip?dl=0 Mind, you'd have to change library and header paths to match your project and Qt locations.