c++qtqt5signals-slots

Pass pointer to itself in emit() in Qt


I have a class handling file transfers. One of the emits is a finished() signal

on the parent side, I'd like to connect this finished() signal to a fileTransferFinished() Slot But how can I know which instance is finished? as there are plenty of them running at the same time..

I know I can make use of the QObject::sender() method to return the emitter, but this way, I can't acces a method of my instance..

qDebug() << "finished " << QObject::sender()->getID();

it says no member named getID in QObject

I'd like to have my pointer inside the Slot function, is that possible?


Solution

  • Look at the prototype of Object::sender() in the documentation:

    QObject * QObject::sender() const
    

    It returns a pointer to QObject instance, hence you will not be able to call getID() method on that return value.

    Cast the return value of Object::sender() to pointer of your class before using it :

    YourClass * sender_obj = qobject_cast<YourClass*>(QObject::sender());
    qDebug() << "finished " << sender_obj->getID();