QTC++ widget中使用QML编写的UI界面
2015-07-06 10:17
585 查看
最近使用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,详细信息如下,
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.
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.
相关文章推荐
- C++重载operator = 和初始化 =
- C++编译问题-检测到 Mac 文件格式: 请将源文件转换为 DOS 格式或 UNIX 格式
- VS2008切换环境(c#,c++,other)
- 【C++】栈空栈满异常处理
- chapter16test2
- C++拷贝构造函数详解
- ubuntu下用Eclipse完成C语言编程练习
- C++的深拷贝与浅拷贝
- 跳跃表(Skip List)的实现及测试 C++实现
- chapter16test1
- C++中关于输入输出流中c_str
- 黑马程序员——C语言之选择排序、折半查找法与二维数组
- C++ Virtual详解
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
- 八大排序算法的C++实现
- C++类型转换关键字 *_cast<type> (expression)
- C++类的所占空间
- C++11中std:bind()与SOCKET bind()冲突问题
- Heat
- C++播放wav音乐和音效