QTC++ widget中使用QML编写的UI界面

         最近使用QT5写了一个UI,控制逻辑采用C++编写,与QML混合编程。主要实现主机和PLC的交互,编写,实现过程都挺顺利的。之前用C++,Qwidget开发过一个类似于桌面的小程序,有没有办法整合进去,通过该程序启动与PLC的通信。于是翻翻Qwidget帮助文档吧,运气还挺好,找到一个

QWidget * QWidget::​createWindowContainer(QWindow * window, QWidget * parent = 0, Qt::WindowFlags flags = 0)
Creates a QWidget that makes it possible to embed window into a QWidget-based application.

The window container is created as a child of parent and with window flags flags.

Once the window has been embedded into the container, the container will control the window's geometry and visibility. Explicit calls to QWindow::setGeometry(), QWindow::show() or QWindow::hide() on an embedded window is not recommended.

The container takes over ownership of window. The window can be removed from the window container with a call to QWindow::setParent().

The window container is attached as a native child window to the toplevel window it is a child of. When a window container is used as a child of a QAbstractScrollArea or QMdiArea, it will create a native window for every widget in its parent chain to allow for proper stacking and clipping in this use case. Applications with many native child windows may suffer from performance issues.

The window container has a number of known limitations:

Stacking order; The embedded window will stack on top of the widget hierarchy as an opaque box. The stacking order of multiple overlapping window container instances is undefined.
Rendering Integration; The window container does not interoperate with QGraphicsProxyWidget, QWidget::render() or similar functionality.
Focus Handling; It is possible to let the window container instance have any focus policy and it will delegate focus to the window via a call to QWindow::requestActivate(). However, returning to the normal focus chain from the QWindow instance will be up to the QWindow instance implementation itself. For instance, when entering a Qt Quick based window with tab focus, it is quite likely that further tab presses will only cycle inside the QML application. Also, whether QWindow::requestActivate() actually gives the window focus, is platform dependent.
Using many window container instances in a QWidget-based application can greatly hurt the overall performance of the application.


然后再网上查找到该文档:http://www.ics.com/blog/combining-qt-widgets-and-qml-qwidgetcreatewindowcontainer#.VJkqj5mDcE,详细信息如下,

Details

The new API provides the following static method:

  QWidget *QWidget::createWindowContainer(QWindow *window, QWidget *parent=0, Qt::WindowFlags flags=0)

The window parameter can be a QQuickView, since it is derived from QWindow. The created window container will be a child of the parent widget and takes over ownership of the QWindow. The QWindow can be removed from the window container with a call to QWindow::setParent().

Once the window has been embedded into the container, it will control the window's geometry and visibility. Explicit calls to QWindow::setGeometry(), QWindow::show() or QWindow::hide() on an embedded window are not recommended.

文档还给出了一个实例。

核心代码如下:

 QQuickView *view = new QQuickView();
    QWidget *container = QWidget::createWindowContainer(view, this);
    container->setMinimumSize(200, 200);
    container->setMaximumSize(200, 200);
    container->setFocusPolicy(Qt::TabFocus);
    view->setSource(QUrl("main.qml"));
    ui->verticalLayout->addWidget(container);

将上述代码移植到一个QWidget或其继承者类,view->setSource(QUrl("main.qml"));导入要显示的QML文件。

QQuickView只能搭配Item,如果你的QML文件的ROOT元素是WIndows类的话,需要更改过来。

如此,就将QML文件嵌入到原先的C++ widget当中了,省去了大幅度修改的麻烦,而且QML开发相对也便捷。

大量使用可能会有副作用:因为Using many window container instances in a QWidget-based application can greatly hurt the overall performance of the application.


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值