网络知识 娱乐 Qml 的QQuickView/Component 转换为UIView——IOS

Qml 的QQuickView/Component 转换为UIView——IOS

前言

用Qt开发ios比较麻烦,而且qt底层也还是原生态框架,所以有时候要将Qt的界面(view)或者其他转化成UIView。

我近期整理草稿看到了之前想写而没写的博客,所以稍微整理下,发布下。

代码

qml Component 转UIView

        QQmlComponent component(engine);
        component.loadUrl(QUrl(QString::fromNSString(url)));
        
        if (!component.isReady() ) {
            m_window = 0;
            qWarning("%s", qPrintable(component.errorString()));
        } else {
            QQuickWindow *window = qobject_cast(component.create());
            m_window = window;
            // We create a dummy parent for now until QWindow::fromWinId() is supported:
            QWindow *dummyParent = new QWindow();
            window->setParent(dummyParent);
            window->setVisible(true);
            UIView *subView = (__bridge UIView *) QGuiApplication::platformNativeInterface()->nativeResourceForWindow("uiview", window);
            [self addSubview: subView];
         }

 获取QQuickWindow(QQuickView的基类)的winId转UIView

void addOCView(QQuickWindow *w)
{
    UIView *view = reinterpret_cast(w->winId());
 
    CGRect  viewRect = CGRectMake(10, 10, 100, 100);
    UIView* myView = [[UIView alloc] initWithFrame:viewRect];
    [myView setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]];
    [view addSubview: myView];
}

 

网友整理的混编示例,很详细可参考


iOS for QT iOS原生界面和QT混编,嵌套,C++和OC的混编 - 简书

官网文档的示例

You have to use the root controller and show the UIPickerImageController on that.
For doing so, you need to use some internal Qt Api:

  • first, in the .pro declare you want to use private headers:
    @
    QT += gui-private
    @
  • then, in your .mm code get the pointer to the root view controller of the Qt app:
    @
    UIView *view = static_cast<UIView >( QGuiApplication::platformNativeInterface()->nativeResourceForWindow("uiview",quickView) );
    UIViewController
     rootCtrl = [[view window] rootViewController];
    @
  • and now, you can show the UIImagePickeController:
    @
    [rootCtrl presentViewController:imagePickerController animated:YES completation:nil]
    @