简体   繁体   English

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

[英]QT Widget Slot Call Not Activated by Separate Thread Signal

I have 5 classes that interact (maintaining professionally, not author). 我有5个互动的类(专业维护,而不是作者维护)。 My problem is that the code that is emitting the signal (there is just one, code below) is never activating the slot (MyWidget::HandleMeasurementChanged). 我的问题是,发出信号的代码(下面只有一个,下面的代码)从未激活插槽(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. 对Observer :: notify的调用率也很高,但是其中大多数将被我未在此处发布的代码过滤掉,并且Emit调用非常少见。 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. 它几乎就像MyWidget类实例没有处理其事件循环一样。 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. 我将连接类型设置为直接连接取得了一些成功,但是由于发出是在一个单独的线程中,并且插槽的生产代码将更新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();
}

This was referenced in the link that Jeremy added in a comment to my question, but just for clarity: 杰里米(Jeremy)在对我的问题的评论中添加的链接中引用了此链接,但仅为了说明:

The solution was to add: 解决方案是添加:

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

immediately before the call to connect. 在通话之前立即连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM