【问题标题】:sending signal with a size_t variable from a thread is not received in the other thread's slot在另一个线程的槽中未接收到从一个线程发送带有 size_t 变量的信号
【发布时间】:2019-11-08 16:07:20
【问题描述】:

我有这两个信号:

void dataSampled(size_t a);
void error(const QString& message);

其他地方:

    m_acquisitionThread = new QThread(this);
    m_acquisitionManager = new AcquisitionManager();
    QObject::connect(m_acquisitionManager, &AcquisitionManager::dataSampled,
                     this,                 &Application::onDataSampled); // this Application pointer
    QObject::connect(m_acquisitionManager, &AcquisitionManager::error,
                     this,                 &Application::showError); // this Application pointer
    m_acquisitionManager->moveToThread(m_acquisitionThread);
    m_acquisitionThread->start();

AcquisitionManager 是一个移动到线程的对象,应用程序位于“主”线程中。

当我向应用程序发送信号时,连接到 dataSampled 的需要 size_t 的插槽不会执行,将 size_t 更改为 int(仅信号,插槽可以保持 size_t)甚至删除它可以解决问题。这真的很奇怪,有人知道为什么没有发送信号吗?在另一个应用程序(但单线程)中,我测试了 size_t 从信号发送到插槽没有问题(但上下文再次不同)。

void AcquisitionManager::executeDataAcquisition()
{
    emit dataSampled(666); // onDataSampled is never executed (only if I change signal type from size_t to an int or something else)
    emit error("foobar"); // Application::showError is always executed !

【问题讨论】:

  • 你试过用 Qt::QueuedConnection 连接吗?这应该会处理好它,而不必将其注册为元类型。

标签: qt


【解决方案1】:

注册 size_t 解决了这个问题:

 qRegisterMetaType<size_t>("size_t");

但是,size_t 不是像 int 这样的原始类型吗?

【讨论】:

  • 可能见bugreports.qt.io/browse/QTBUG-42106std::size_t&lt;cstddef&gt; 和其他头文件中定义为某些真实原始类型的别名。它不是完全原始的,尽管它内置sizeof 运算符的结果...这就是C++ 中的生活,其中stdlib 的某些部分对于使用核心功能是必不可少的。
  • @underscore_d 感谢您的澄清!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
相关资源
最近更新 更多