QT Widget 插槽调用未由单独的线程信号激活

发布于 2024-12-07 17:04:48 字数 1981 浏览 5 评论 0原文

我有 5 个交互类(专业维护,而不是作者)。我的问题是发出信号的代码(只有一个,下面的代码)永远不会激活插槽(MyWidget::HandleMeasurementChanged)。该系统具有很大程度的复杂性。我试图减少这种情况,但认为复杂性可能会导致这个问题。对 Observer::notify 的调用率也很高,但其中大部分会被我未在此处发布的代码过滤掉,并且 Emit 调用相当罕见。如果有人能帮我指出为什么该插槽没有被激活,我将非常感激。它几乎就像 MyWidget 类实例没有处理其事件循环一样。我在将连接类型设置为直接连接方面取得了一些成功,但由于发出是在单独的线程中,并且插槽的生产代码将更新 UI,因此我已将其排除为最终解决方案。

class IObserver { public: virtual void notify()=0; };

class ExternalMeasurement { ... };
class Measurement { public: Measurement(ExternalMeasurement source); };

class Observer : public QThread, public IObserver
{
signals:
   void MeasurementChanged(boost::shared_ptr<Measurement> measurement);   
public:
   //called by 3rd party in separate thread
   virtual void notify(ExternalMeasurement measurement)
   {
      _measurement_ = 
           boost::shared_ptr<Measurement>(new Measurement(measurement));
      emit MeasurementChanged(_measurement);
   }
private:
   boost::shared_ptr<Measurement> _measurement_;
};

class MyWidget : public QWidget
{
private:
   Component _component_;
public slots:
   void HandleMeasurementChanged(boost::shared_ptr<Measurement> measurement);
public:
   MyWidget(Component * component_)
};

MyWidget::MyWidget(Component * component_)
{
   _component_ = component_;
   connect(
      _component_->_observer_, 
      MeasurementChanged(boost::shared_ptr<Measurement> measurement),
      this,
      HandleMeasurementChanged(boost::shared_ptr<Measurement> measurement));
}

class Component
{
private:
   QApplication * _application_;
   MyWidget * _widget_;
   Observer * _observer_;
public:
   void MainFunc();
}

void Component::MainFunc()
{
   _observer_ = new Observer();
   ...
   _application_ = new QApplication(...);
   ...
   _widget_ = new MyWidget(...);
   ...
   _widget_->show();
   _application_->exec();
}

I have 5 classes that interact (maintaining professionally, not author). My problem is that the code that is emitting the signal (there is just one, code below) is never activating the slot (MyWidget::HandleMeasurementChanged). This system has a large degree of complexity. I have tried to reduce that, but think the complexity likely contributes to the problem. There is also a high rate of calls to Observer::notify, but most of these will get filtered out by code that I have not posted here and the Emit calls are fairly rare. If anyone could help point me to why the slot is not getting activated, I'd really appreciate it. It is almost acting like the MyWidget class instance is not processing its event loop. I have had a little success setting the connect type to Direct Connection, but since the emit is in a separate thread and the production code for the slot will update the UI I have ruled that out as a final solution.

class IObserver { public: virtual void notify()=0; };

class ExternalMeasurement { ... };
class Measurement { public: Measurement(ExternalMeasurement source); };

class Observer : public QThread, public IObserver
{
signals:
   void MeasurementChanged(boost::shared_ptr<Measurement> measurement);   
public:
   //called by 3rd party in separate thread
   virtual void notify(ExternalMeasurement measurement)
   {
      _measurement_ = 
           boost::shared_ptr<Measurement>(new Measurement(measurement));
      emit MeasurementChanged(_measurement);
   }
private:
   boost::shared_ptr<Measurement> _measurement_;
};

class MyWidget : public QWidget
{
private:
   Component _component_;
public slots:
   void HandleMeasurementChanged(boost::shared_ptr<Measurement> measurement);
public:
   MyWidget(Component * component_)
};

MyWidget::MyWidget(Component * component_)
{
   _component_ = component_;
   connect(
      _component_->_observer_, 
      MeasurementChanged(boost::shared_ptr<Measurement> measurement),
      this,
      HandleMeasurementChanged(boost::shared_ptr<Measurement> measurement));
}

class Component
{
private:
   QApplication * _application_;
   MyWidget * _widget_;
   Observer * _observer_;
public:
   void MainFunc();
}

void Component::MainFunc()
{
   _observer_ = new Observer();
   ...
   _application_ = new QApplication(...);
   ...
   _widget_ = new MyWidget(...);
   ...
   _widget_->show();
   _application_->exec();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2024-12-14 17:04:48

杰里米在我的问题的评论中添加的链接中引用了这一点,但只是为了清楚起见:

添加: 。

qRegisterMetaType<shared_ptr<Measurement> >("shared_ptr<Measurement>");

解决方案是在调用连接之前立即

This was referenced in the link that Jeremy added in a comment to my question, but just for clarity:

The solution was to add:

qRegisterMetaType<shared_ptr<Measurement> >("shared_ptr<Measurement>");

immediately before the call to connect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文