从本机iOS gui切换到Qt gui

Switching from native iOS gui to Qt gui

本文关键字:gui Qt iOS 本机      更新时间:2023-10-16

我目前有一个本机iOS GUI和一个Qt GUI。我正试着从一个换到另一个。

需要明确的是:当我点击原生GUI上的按钮时,我希望Qt GUI显示出来,反之亦然。

我已经找到了我必须添加哪些库才能使用Qt Stuff。我在AppDelegate.mm文件中创建了一个QApplication

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
    // receive int argc, and char** argv for the QApplication.
    _qApp = new QApplication(_argc, _argv);
}

此外,我的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();
}

在本机iOS GUI中按下按钮时,我正在ViewController.mm中调用createQtGUI方法。代码运行时没有抛出任何错误,但是:

Qt GUI不显示。应用程序仍然显示本机gui,而不切换到Qt gui

有人知道怎么解决这个问题吗?

我终于找到了缺失的东西:

Qt对象提供了一个名为winId()的方法。此方法返回一个WId,它实际上是(在iOS上)UIView*

您必须将UIView*作为子视图添加到主视图中。


为了实现这一点,我更改了createQtGUI方法如下:

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

在我的ViewController.mm(我称之为方法)中:

- (IBAction)ButtonClicked:(id)sender {
    UIView* newView = (__bridge UIView*)reinterpret_cast<void*>(createQtGUI());
    [self.view addSubview:newView];
}

注意:双重强制转换(__bridge UIView*)reinterpret_cast<void*>(...)是必要的,因为在Objective-C++中不能仅从WId强制转换为UIView*