简体   繁体   English

从原生iOS gui切换到Qt gui

[英]Switching from native iOS gui to Qt gui

I currently have a native iOS GUI and a Qt-GUI. 我目前有一个原生的iOS GUI和一个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. 要明确:当我点击本机GUI上的按钮时,我希望Qt-GUI显示,反之亦然。

I already found out which libraries i have to add to be able to use the Qt-Stuff. 我已经找到了我必须添加哪些库才能使用Qt-Stuff。 I created a QApplication in the AppDelegate.mm file: 我在AppDelegate.mm文件中创建了一个QApplication

- (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: 此外,我的Qt应用程序看起来(此刻)像这样:

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. 当我在本机iOS GUI中按下按钮时,我在我的ViewController.mm调用createQtGUI方法。 The code runs without throwing any error, but: 代码运行时不会抛出任何错误,但是:

The Qt-GUI is not shown. Qt-GUI未显示。 The application still shows the native gui without switching to the Qt-GUI. 应用程序仍然显示本机gui而不切换到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() . Qt-Objects提供了一个名为winId()的方法。 This method returns a WId which actually is (on iOS) an UIView* . 这个方法返回一个实际上是(在iOS上) UIView*WId

You have to add that UIView* as Subview to your main view. 您必须将UIView*作为Subview添加到主视图中。


In order to achieve that I changed my createQtGUI method as follows: 为了实现这一点,我改变了我的createQtGUI方法,如下所示:

WId createQtGUI() {
    ... // nothing changed here (only at the end)
    window->show();

    return window->winId();
}

And In my ViewController.mm (where I call my method): 在我的ViewController.mm (我调用我的方法):

- (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++. 注意:双重演员(__bridge UIView*)reinterpret_cast<void*>(...)是必要的,因为你不能在Objective-C ++中从WIdUIView*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM