Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using Qt::connect(), get senders type



  • Hello,

    I have 3 objects derived from same base class,

    Com c;
    Mov m;
    Sub s;

    in that base classe there is a SIGNAL connectedChanged(bool)

    I want to connect that SIGNAL to a SLOT of another object :

       QObject::connect(&c,SIGNAL(connectedChanged(bool)),&logger,SLOT(writeLog(bool)));
       QObject::connect(&m,SIGNAL(connectedChanged(bool)),&logger,SLOT(writeLog(bool)));
       QObject::connect(&s,SIGNAL(connectedChanged(bool)),&logger,SLOT(writeLog(bool)));
    

    can my writeLog(bool) know who is the sender c ? m ? or l ?

    Thx



  • QObject::sender()->metaObject()->className()



  • QObject::sender()->metaObject()->className()



  • change writeLog to writeLog(bool,const QString&)

    QObject::connect(&c,&BaseClass::connectedChanged,&logger,std::bind(&LoggerClass::writeLog,&logger,std::placeholders::_1,QStringLiteral("c"));
    QObject::connect(&m,&BaseClass::connectedChanged,&logger,std::bind(&LoggerClass::writeLog,&logger,std::placeholders::_1,QStringLiteral("m"));
    QObject::connect(&s,&BaseClass::connectedChanged,&logger,std::bind(&LoggerClass::writeLog,&logger,std::placeholders::_1,QStringLiteral("s"));
    


  • thx @VRonin. I just saw the the new syntax..


Log in to reply