Results 1 to 2 of 2

Thread: problem with QWeakPointer, correct usage of smart poiners ?

  1. #1
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default problem with QWeakPointer, correct usage of smart poiners ?

    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.

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem with QWeakPointer, correct usage of smart poiners ?

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

    The QWeakPointer has no overloaded dereferencing operator like QSharedPointer::operator->

Similar Threads

  1. qt smart pointer
    By sajis997 in forum Newbie
    Replies: 4
    Last Post: 12th April 2011, 21:59
  2. resize problem, example and document not correct?
    By zhxys in forum Qt Programming
    Replies: 1
    Last Post: 21st January 2011, 09:31
  3. Smart Pointer Design
    By lexfridman in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2011, 01:41
  4. Qt Creator Problem selecting the correct mkspec/qmake.conf file
    By weaver4 in forum Qt Tools
    Replies: 1
    Last Post: 2nd December 2010, 13:37
  5. Qt 4.6: Which Qt smart pointer to use for this case?
    By ShaChris23 in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2009, 03:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.