Results 1 to 20 of 21

Thread: QSharedPointer with signals and slots

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QSharedPointer with signals and slots

    I have recently started using QSharedPointer for a particular type of object which is shared between many classes. Previously i had just used a raw pointer and managed the creation and deletion of the objects in one place. But now i realise it will be much easier (and actually necessary) to do reference counting.

    For some reason, there are very few examples out there on how to use QSharedPointer, so i find myself posting here.

    One problem i have ran into is using signals and slots with the objects that are shared-pointed-to. Previously i had done this:
    Qt Code:
    1. MyObject* object; // Subclass of QObject.
    2. AnotherObject* something;
    3. ...
    4. connect(object, SIGNAL(updated()), something, SLOT(update())); // Notify when my object has changed
    To copy to clipboard, switch view to plain text mode 

    But now that i have changed MyObject* to QSharedObject<MyObject> (typedef'd as MyObjectPtr), the compiler complains:
    error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'MyObjectPtr' to 'const QObject *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    I read that i need to use Q_DECLARE_METATYPE and qRegisterMetaType, but i'm not sure about that.
    QObject::connect takes a pointer, but here i am passing a QSharedPointer object. Would doing the above allow that type to be passed? Do i still need to do that if the MyObject class is already a subclass of QObject?
    And is it safe to just pass the raw pointer obtained from object.data()?
    [Window Detective] - Windows UI spy utility
    [War Thunder]

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSharedPointer with signals and slots

    But now that i have changed MyObject* to QSharedObject<MyObject> (typedef'd as MyObjectPtr), the compiler complains:
    whats a QSharedObject ?


    Added after 4 minutes:


    And is it safe to just pass the raw pointer obtained from object.data()?
    Why wouldn't it be?
    Last edited by amleto; 25th June 2012 at 08:21.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 1
    Last Post: 10th August 2011, 16:46
  2. about signals and slots
    By Sandip in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2008, 16:02
  3. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  4. help with signals and slots
    By superutsav in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2006, 12:49
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 08:12

Tags for this Thread

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.