I have classes, corresponding to devices connected to a controller, which are saved
in a vector in the controller class.

I want to have full control over the devices within the controller, which means they must not be copied and shall not be deleted.

The base class controller saves pointers to base class devices. With real controllers (derived) and real devices (derived) it saves pointers to derived device classes - that is why I want to use pointers.

My idea was to use QSharedPointer and QWeakPoiner. Basically I want to know if this is the right idea, and why my code fails.

The devices (CommonPositioningStage) are stored in the CommonPositioningController-class (the base class)
Qt Code:
  1. class CommonPositioningControllerPrivate
  2. {
  3. public:
  4. CommonPositioningControllerPrivate()
  5. {
  6. // automatically delete all instances of
  7. // CommonPositioningStage
  8. stageProperties.clear();
  9. }
  10. QVector<QSharedPointer<CommonPositioningStage> > stageProperties;
  11. };
  12.  
  13. CommonPositioningController::CommonPositioningController()
  14. : d(new CommonPositioningControllerPrivate)
  15. {
  16. }
  17.  
  18. ...
  19. bool CommonPositioningController::addStage(CommonPositioningStage * newStage)
  20. {
  21. if (newStage == 0) return false;
  22.  
  23. // check if axis does not exist yet
  24. ...
  25. // add only if axis does not exist
  26. if (!axisAlreadyExists) {
  27. d->stageProperties.push_back(
  28. QSharedPointer<CommonPositioningStage>(newStage));
  29. }
  30. return !axisAlreadyExists;
  31. }
  32.  
  33. QSharedPointer<CommonPositioningStage> CommonPositioningController::stage()
  34. {
  35. return d->stageProperties[m_currentListIndex];
  36. }
To copy to clipboard, switch view to plain text mode 

Usage in derived Controller class
Qt Code:
  1. void QMicosPolluxController::setDevice(int axis, QMicosPolluxDevice * device)
  2. {
  3. if (device) {
  4. addStage(device);
  5. }
  6. }
To copy to clipboard, switch view to plain text mode 

Usage in ui classes
Qt Code:
  1. class WidgetMicosPolluxControllerMove : public QWidget
  2. ...
  3. private:
  4. QMicosPolluxController * m_controller;
  5. QWeakPointer<QMicosPolluxDevice> m_device;
  6. ...
  7. };
  8.  
  9. void WidgetMicosPolluxControllerMove::setController(QMicosPolluxController * parentController)
  10. {
  11. m_controller = parentController;
  12. if (m_controller->stageCount() > 0) {
  13. m_device = m_controller->stage();
  14. }
  15. }
To copy to clipboard, switch view to plain text mode 

However acces to m_device fails, because non of the class functions are available

Qt Code:
  1. m_device.limitPosition()
To copy to clipboard, switch view to plain text mode 

WidgetMicosPolluxControllerMove.cpp:127: error: 'class QWeakPointer<QMicosPolluxDevice>' has no member named 'limitPosition'
Now how do I access the QMicosPolluxDevice class in QWeakPointer and is the whole appreach correct?

The only other way i can think of is using plain pointers. It is dangerous, but causes no problems on compilation.