我有5个相互作用的课程(专业维护,而不是作者)。我的问题是发出信号的代码(下面只有一个代码)永远不会激活插槽(MyWidget :: HandleMeasurementChanged)。该系统具有很大的复杂性。我试图减少这种情况,但认为复杂性可能会导致问题。对Observer :: notify的调用率也很高,但其中大部分将被我未在此处发布的代码过滤掉,并且Emit调用相当罕见。如果有人能帮我指出为什么插槽没有被激活,我真的很感激。它几乎就像MyWidget类实例没有处理它的事件循环。我将连接类型设置为Direct Connection有一点成功,但由于emit是在一个单独的线程中,并且插槽的生产代码将更新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();
}
答案 0 :(得分:1)
Jeremy在我的问题评论中添加的链接中引用了这一点,但为了清楚起见:
解决方案是添加:
qRegisterMetaType<shared_ptr<Measurement> >("shared_ptr<Measurement>");
紧接呼叫连接之前。