Results 1 to 12 of 12

Thread: Questions about signals/slots and QObject lifetimes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Questions about signals/slots and QObject lifetimes

    I'm trying to find documentation about the deep nature of Signals and Slots (which are well documented in the source)...

    for instance, connect() is frequently called from 'this', but if 'this' calls:

    Qt Code:
    1. connect(that, SIGNAL(_something()), other, SLOT(_else()));
    To copy to clipboard, switch view to plain text mode 

    then is the connection connected to the lifetime of 'this', or is it attached to an internal database so that deleting 'this' will allow the connection to persist?

    I'm hoping that deleting 'this' will destroy the connection, but 'this' is separate from 'that' and 'other', so the question is, are connections connected to the creator or the objects?
    Last edited by themolecule; 23rd May 2014 at 15:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Questions about signals/slots and QObject lifetimes

    If you look at the signature of the connect method you used here, you will see that it is a static method of QObject.

    So the "this" object is not involved at all with this connection.

    The connection will persist as long as its sender and receiver exist or it is undone using QObject::disconnect().

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,325
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Questions about signals/slots and QObject lifetimes

    If you look at the signature of the connect method you used here, you will see that it is a static method of QObject.
    Well, yes, but the OP's question really boils down to: if one of the instances involved in the connection (either the sender or the receiver) is destroyed, is the connection also destroyed?

    The answer to that is "yes". If that was not the case, things would likely be crashing all over the place due to slots being invoked with dangling, deleted pointers.

    QObject has a QObject::destroyed() signal which you can use to detect when an object you are interested in has been (or is about to be) destroyed. For example, if you keep a pointer to some QObject instance in a data structure, and that QObject's lifetime was controlled by something else, you'd want to make sure that pointer was still valid before using it. If you connect to the instance's destroyed() signal, you can be notified when it goes out of scope and can take appropriate action (like set your stored pointer variable to NULL).

Similar Threads

  1. Replies: 2
    Last Post: 18th April 2013, 12:15
  2. Replies: 1
    Last Post: 7th February 2011, 15:44
  3. Signals and Slots - Moc QObject Problem
    By GBayo1 in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2010, 16:35
  4. Signals and Slots questions
    By Janek in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2009, 21:47
  5. qobject connect questions
    By VireX in forum Newbie
    Replies: 5
    Last Post: 20th May 2007, 09:04

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
  •  
Qt is a trademark of The Qt Company.