Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching from native iOS gui to Qt gui

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 the AppDelegate.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 my ViewController.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?

like image 916
ParkerHalo Avatar asked Apr 07 '16 14:04

ParkerHalo


1 Answers

I've finally found out what was missing:

The Qt-Objects provide a method called winId(). This method returns a WId which actually is (on iOS) an UIView*.

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 from WId to UIView* in Objective-C++.

like image 59
ParkerHalo Avatar answered Oct 21 '22 11:10

ParkerHalo