Results 1 to 5 of 5

Thread: Signal from QList Object to Slot from parent

  1. #1
    Join Date
    Feb 2012
    Location
    Germany
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Signal from QList Object to Slot from parent

    I want to relay/concatenate a signal from an object I put into a QList to its parent to trigger a redraw of the parent object. But where to put the connect()?
    Qt Code:
    1. void Desktop::addTray(Tray tray) {
    2. trayList.append(tray);
    3. }
    4.  
    5. void Tray::setColor(int number, QColor color) {
    6. pointList[number].setColor(color);
    7. emit dataChanged();
    8. }
    To copy to clipboard, switch view to plain text mode 
    If I append a new tray object to the desktop I copy the object not a pointer to it. So If I would write the connect() after line 2, I wouldn’t connect to the object within the trayList.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal from QList Object to Slot from parent

    In order for Tray to have slots, it must be derived from QObject. If you tried to compile your example you would get an error as QObject-derived classes are non-copiable. What you need to do is store pointers to the Tray objects in the list and manage the memory of course.

  3. #3
    Join Date
    Feb 2012
    Location
    Germany
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Signal from QList Object to Slot from parent

    Thanks, I realised that I have to save pointers to my Tray objects. I create this objects dynamically, while parsing a file.
    But how to manage the memory?
    If I'm leaving the parsing function, every object I created is deleted and every pointer to it invalid. If I declare it static I never can delete or free it.


    Added after 53 minutes:


    No I try to put my Tray Objects on the heap and put a pointer to them in my QList. If I remove them from the list, I delete them from the heap
    Last edited by Rocken; 5th April 2012 at 14:15.

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal from QList Object to Slot from parent

    No I try to put my Tray Objects on the heap and put a pointer to them in my QList. If I remove them from the list, I delete them from the heap
    Exactly. Alternatively you can store QSharedPointers in the QList instead, or better yet, use a container class for pointers that calls delete on the erased elements. There is no such container class in Qt 4, but some other libraries provide them (such as Boost's Pointer Container Library).

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signal from QList Object to Slot from parent

    If Tray object has a parent (using Qt parent/child mechanism) then you can
    Qt Code:
    1. QMetaObject::invokeMethod( Tray::parent(), "update" );
    To copy to clipboard, switch view to plain text mode 
    Which will trigger repaint event.

Similar Threads

  1. SIGNAL/SLOT - Object scope?
    By StarShaper in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2012, 13:35
  2. Connect signal from base to slot of sub-sub-object
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 30th October 2008, 19:54
  3. Signal to specific object slot
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 27th December 2007, 15:51
  4. Signal-Slot, object instances
    By ct in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2006, 19:08
  5. queued signal/slot connection with QList<int>
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2006, 14:32

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.