2
WMIQuery::wmiquery(WMI::WMITable* table, const QString& query, WMI::ProgressIndicator* progressIndicator)

这是函数签名。我打电话给它QtConcurrent::run

QFuture<quint32> future = QtConcurrent::run(WMI::WMIQuery::wmiquery, _table, query);

架构非常简单。查询将返回的预期行数是已知的。查询是并行运行的,并且在每条记录上提取一行添加到 table: WMI::WMITable* WMI::WMITable一个简单的 QObject 表数据结构。它在行添加时发出rowsAboutToBeInserted(QModelIndex, int, int)和。rowsInserted(QModelIndex, int, int)

另一方面ProgressIndicator,在主线程上实例化并table传递给它的ctor. WMI::WMIQuery::wmiquery()它从到获得预期的总行数ProgressIndicator::setRecordCount(quint64 count)。它有一个插槽rowAdded(),可以通过做一些简单的数学计算出 100 的进度。在它的ctor中它连接

connect(_table, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(rowAdded()));

我怎么想。当WMI::WMIQuery::wmiquery()我在不同的线程(on QThreadPool)上运行时,这个连接是一个跨线程排队连接。我对么 ?

我在运行时收到以下错误

QObject::connect: 无法对“QModelIndex”类型的参数进行排队
(确保使用 qRegisterMetaType() 注册了“QModelIndex”。)

我该怎么办 ?因为我SLOT(rowAdded())不需要 3 个参数,SIGNAL(rowsInserted(QModelIndex,int,int))我是否应该发出另一个信号rowInserted(),并在我发出时发出它,并将它rowsInserted(QModelIndex,int,int)用于SIGNAL这个连接

你可能会问为什么我rowsInserted(QModelIndex,int,int)在表数据结构中使用类似模型的信号。因为我也有一个连接到这个表的模型。这也将逐行更新。但是我认为这在这方面是不重要的。

4

2 回答 2

2

在使用非平凡参数类型(如QModelIndex)跨线程边界发出信号之前,您必须首先调用它:

qRegisterMetaType<QModelIndex>("QModelIndex");

这让 Qt 能够跨线程边界发出信号。

通常,您会在调用之前,但您的实例化之后main(),或仅运行一次的地方执行此操作。emitQApplication

这仅对非平凡的类型是必需的。例如,像这样的信号不需要你打电话qRegisterMetaType()

signals:
   void mySignal(int foo, int bar);

But a signal like this does require qRegisterMetaType():

signals:
   void mySignal(QModelIndex);

For more info, see the Qt docs here: http://doc.qt.nokia.com/latest/qmetatype.html#qRegisterMetaType

于 2011-07-11T19:53:47.557 回答
2

I know this is rather late, but I wanted to be sure someone mentioned it: QModelIndex is not meant to be queued, for the same reason that it's not meant to be stored and used later in other ways. That is, if the model changes before you use the QModelIndex, you will get undefined behavior. If you need queued events with model indices, you should probably use QPersistentModelIndex. Not really relevant to the original question, but may be of use to someone who lands here.

于 2016-06-24T22:03:23.187 回答