Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Adding a QWindow to UIKit application on iOS
QtWS25 Last Chance

Adding a QWindow to UIKit application on iOS

Scheduled Pinned Locked Moved Solved Mobile and Embedded
iosxcodeswiftqt 5.10objective-c
2 Posts 1 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eliseev
    wrote on 21 Sept 2018, 04:58 last edited by
    #1

    Hi all,

    This is essentially a continuation of this topic:
    https://forum.qt.io/topic/65979/calling-qt-gui-from-native-ios-gui-vice-versa

    I 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.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      eliseev
      wrote on 21 Sept 2018, 08:30 last edited by
      #2

      Ok, the problem solved itself with update to Qt 5.11.2 (while running iOS12). Thanks for reading.

      1 Reply Last reply
      0

      1/2

      21 Sept 2018, 04:58

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved