qt - 从 QThread 工作类发出的信号没有到达
问题描述
我有这个简化的代码:
class MyCustomObject {
};
class DeviceConnection : public QObject {
Q_OBJECT
public:
explicit DeviceConnection(QObject* const parent = nullptr);
signals:
void readFinished(MyCustomObject result);
public slots:
void readFromDevice();
};
DeviceConnection::readFromDevice() {
/* ... */
emit readFinished(MyCustomObject());
}
void MainWindow::on_actionRead_triggered() {
QThread* const thread = new QThread(this);
DeviceConnection* const connection = new DeviceConnection();
connection->moveToThread(thread);
thread->start();
connect(connection, &DeviceConnection::readFinished, this, [=](MyCustomObject data) {
/* This never runs. */
connection->deleteLater();
thread->quit();
});
QTimer::singleShot(0, connection, &DeviceConnection::readFromDevice);
}
这开始阅读就好了。我可以在调试器中看到我正在到达这emit
条线,并且我正在这条线中到达那里。但我也可以在调试器和代码行为中看到,readFinished
从未调用过 lambda。对于不是 lambdas 的插槽也是如此。有什么问题?
编辑:当我不使用额外的线程时,此代码运行良好,但当然它在readFromDevice()
运行时会阻塞主线程。
解决方案
我想到了。不幸的是,当我第一次问这个问题时,我简化了重要的部分,但我只是把它重新编辑了。
问题是MyCustomObject
不能在 Qt 消息队列中排队。为此,您需要运行以下命令:
qRegisterMetaType<MyCustomObject>("MyCustomObject");
或者
// ideally just after the definition for MyCustomObject
Q_DECLARE_METATYPE(MyCustomObject);
// any time before you want to enqueue one of these objects
qRegisterMetaType<MyCustomObject>();
推荐阅读
- discord - 预期声明或声明。ts(1128) (11, 1)
- vba - VBA 提取第一个 Google 搜索结果 URL 错误
- python - 我如何输入路径并在同一路径中创建所有 csv 文件的 hdf5 文件?
- laravel - 如何生成 PayPal 订单支付链接到电子邮件
- java - 程序读取七个整数值并打印出每个值的出现次数
- python - python pandas中奇偶数的总和
- python - 如何从 foo.py 访问 show()?
- swift - 插入对象后不能更改主键
- excel - 使用 AWK 实现 Excel if-like 函数
- python - Ansible 正则表达式从字符串中删除项目