How to know which is the class that sends the signal in qt ?



  • My question is about how to know which is the class that sends the signal because what i want to do is specified to the class that has sent the signal
    is it possible to do that ?
    this is a prototype of what i intend to do

    @ if (signal comes from class 1)
    {do specified actions }
    else if (comes from class 2)
    {do something else } @

    thanks




  • Moderators

    Check "this":http://qt-project.org/doc/qt-4.8/qobject.html#sender.
    This only works if you use Qt::DirectConnection and your application is not "threaded".

    @
    slot() {
    qDebug() << sender()->metaObject()->className();
    }
    @


  • Lifetime Qt Champion

    Hi,

    It is, you can use "QObject::sender()":http://qt-project.org/doc/qt-4.8/qobject.html#sender coupled with "qobject_cast":http://qt-project.org/doc/qt-4.8/qobject.html#qobject_cast

    But are you sure it's the right design ?



  • how to translate this and use qt signal mapper with it ???

    @ connect( ui->welcome, SIGNAL(changeStackedWidgetIndex(int)),
    ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    connect( ui->credentials, SIGNAL(changeStackedWidgetIndex(int)),
    ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    connect( ui->config, SIGNAL(changeStackedWidgetIndex(int)),
    ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    connect( ui->syncwindow, SIGNAL(changeStackedWidgetIndex(int)),
    ui->stackedWidget, SLOT(setCurrentIndex(int)) );
    connect( ui->loaduser, SIGNAL(changeStackedWidgetIndex(int)),
    ui->stackedWidget, SLOT(setCurrentIndex(int)) );@


  • Lifetime Qt Champion

    When is changeStackedWidgetIndex emitted ?
    What are you trying to achieve ?



  • If you really need to do different things depending on the sender, why don't you create 5 different slots?

    @connect( ui->welcome, SIGNAL(changeStackedWidgetIndex(int)),
    this, SLOT(setWelcomeIndex(int)) );
    connect( ui->credentials, SIGNAL(changeStackedWidgetIndex(int)),
    this, SLOT(setCredentialsIndex(int)) );
    connect( ui->config, SIGNAL(changeStackedWidgetIndex(int)),
    this, SLOT(setConfigIndex(int)) );
    connect( ui->syncwindow, SIGNAL(changeStackedWidgetIndex(int)),
    this, SLOT(setSyncwindowIndex(int)) );
    connect( ui->loaduser, SIGNAL(changeStackedWidgetIndex(int)),
    this, SLOT(setLoaduserIndex(int)) );

    void YourClass::setWelcomeIndex(int idx)
    {
    someCodeSpecificToWelcome(idx);
    commonSetIndex(idx);
    }

    void YourClass::setCredentialsIndex(int idx)
    {
    someCodeSpecificToCredential(idx);
    commonSetIndex(idx);
    }

    void YourClass::commonSetIndex(int idx)
    {
    someCodeCommonToEveryone(idx);
    ui->stackedWidget->setCurrentIndex(idx);
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.