带有多个线程的QML C ++属性绑定 [英] qml c++ property bindings with multiple threads

查看:684
本文介绍了带有多个线程的QML C ++属性绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个模块化应用程序.它有一个Core和几个模块,其中一个是gui模块.这些模块可以通过命令行启动:

I am creating an modular application. It has a Core, and a few modules, amongs which is a gui module. These modules can be started via the command line:

myApp gui=qml=qmlFile.qml connection=serial=/dev/ttyS1

将使用一个(也可以是多个)gui和一个串行连接来启动该应用程序.总是有一个Router对象,用于处理连接. Guis是由如下所示的类加载的:

will start the app with one (multiple is also possible) gui and one serial connection. There is always one Router object, which handles the connection. Guis are loaded by a class that looks like this:

class Gui :QObject{
  Core* core;
  public:
  QQmlApplicationEngine engine;
  public slots:
  void start(){
    engine.load("qrc:/gui/from/command/line.qml");
  }

在gui中,我有一个很好的qml,称为ConnectionController{},这是添加/删除连接的简便方法.但这只是同类中的第一个:最后,每个模块都应该有一个qml组件来控制它.

In the gui i have a nice bit of qml called ConnectionController{} which is an easy way of adding/removing connections. But this is only the first of it's kind: in the end every module should have a qml component for controlling it.

这一切都很好,但是现在我试图添加多个线程.我想要一个RouterThreadGuiThread.本身仅仅是将Router(由Core生成)的创建更改为以下内容的问题:

This all works fine, but now i am trying to add multiple threads. I want to have a RouterThread and a GuiThread. This in itself was simply a matter of changing the creation of the Router (by the Core) to this:

m_router = new Router(this /*not as parent, just as pointer*/);
m_router->moveToThread(&routerThread);

(gui仍然在主线程中) 此后,除了qml属性绑定之外,其他所有东西都仍然可以正常工作.

(gui's still live in the main thread for now) After this everything still works fine, except for qml property bindings.

QQmlEngine: Illegal attempt to connect to SerialConnection(0x7f58d00013a0) that is in a different thread than the QML engine

因为Gui的子级QQmlEngine engine居住在guiThread中,而SerialConnection居住在routerThread中.
RouterGui之间的信号和插槽(没有qml)可以正常工作,因为默认情况下,它们被qt异步处理. (只要定义了适当的类型.)

Because Gui and it's child QQmlEngine engine live in the guiThread while the SerialConnection lives in the routerThread.
Signals and slots between Router and Gui (without qml) do work, because they are handled by default as asynchronous by qt. (as long as the appropriate types are defined.)

推荐答案

好吧,不,您不能从gui访问线程对象,也不应该尝试这样做.

Well, no, you cant access threaded objects from your gui and should not try to either.

您需要构建一个驻留在暴露给qml的主线程中的c ++模型.在此模型内部,您可以通过队列连接连接到线程发出的信号,并将它们链接到模型自己的信号. 另外,还应确保从另一个线程拥有的对象读取数据时,所有内容均已正确互斥,否则将读取垃圾.

You need to build a c++ model that lives in the main thread that is exposed to qml. Inside this model you can connect to signals emitted by your threads via a queuedconnection and link them to the models' own signals. Also you should make sure that when you read data from an object owned by another thread, everything is properly mutexed or you will be reading garbage.

这篇关于带有多个线程的QML C ++属性绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆