PDA

View Full Version : Signal from QList Object to Slot from parent



Rocken
5th April 2012, 11:59
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()?


void Desktop::addTray(Tray tray) {
trayList.append(tray);
}

void Tray::setColor(int number, QColor color) {
pointList[number].setColor(color);
emit dataChanged();
}

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.

yeye_olive
5th April 2012, 13:12
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.

Rocken
5th April 2012, 14:15
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

yeye_olive
6th April 2012, 15:05
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).

Spitfire
9th April 2012, 14:34
If Tray object has a parent (using Qt parent/child mechanism) then you can
QMetaObject::invokeMethod( Tray::parent(), "update" ); Which will trigger repaint event.