Qml 的QQuickViewComponent 转换为UIView——IOS

Qml 的QQuickViewComponent 转换为UIView——IOS,第1张

前言

用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>( QGuiApplication::platformNativeInterface()->nativeResourceForWindow("uiview",quickView) );
UIViewController rootCtrl = [[view window] rootViewController];
@and now, you can show the UIImagePickeController:
@
[rootCtrl presentViewController:imagePickerController animated:YES completation:nil]
@

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/990232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存