Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    5
    7
    1554
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      sliver_twist last edited by

      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

      1 Reply Last reply Reply Quote 0
      • Q
        qxoz last edited by

        Hi!
        Look at this "thread":http://qt-project.org/forums/viewthread/12877

        1 Reply Last reply Reply Quote 0
        • raven-worx
          raven-worx Moderators last edited by

          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();
          }
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • S
              sliver_twist last edited by

              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)) );@

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • J
                  JulienMaille last edited by

                  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);
                  }@

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post