0
votes

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();
}
1
I suspect you will find the answer to your problem [here][1]. [1]: stackoverflow.com/questions/4807484/…Jeremy Friesner
I had already read that one Jeremy. It seemed promising, but that question was about how to get a const & modification of what I'm doing working. The only directly relevant thing I found in that thread was a statement that what I'm doing should work fine.PatrickV
Actually Jeremy, that was it. I had way too much debug output to see the very clear error message. Adding a qRegisterMetaType call to register the shared_ptr templated type fixed my problem. Thanks for the "go read that again" that I clearly needed.PatrickV

1 Answers

1
votes

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.