Important: Please read the Qt Code of Conduct -

[SOLVED] 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()?

    void Desktop::addTray(Tray tray) {

    void Tray::setColor(int number, QColor 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.

  • since Tray is QObject subclass, "it can't be copied":
    when you connect, you connect with the original object you created

  • You're right.. I didn't inherit from QObject. I have to change my QList and store pointers, not objects.
    But how can I prevent that the objects get deleted or freed?
    The Tray-Objects where created dynamically while parsing a file. If the parser function is exited, every Tray object will be deleted or freed.
    How can I prevent them from deletion? I thought on "static" but I want to delete the Tray objects when the QList gets cleared which would be possible with static declaration.

  • well this is up to you
    but you can also make the function using references, addTray(Tray &trayObject), this also will work

  • Oh, you're fast! I edited my last post

  • create your objects on the heap (using new) not on the stack, this way you have to take care of deleting them and they won't be destroyed when the scope they were created in is closed.

  • OK thanks, I will rewrite my code and try to put the Trays on the heap and save pointers them in the trayList

Log in to reply