Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to know which is the class that sends the signal in qt ?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 1.8k Views
  • 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 Offline
    S Offline
    sliver_twist
    wrote on last edited by
    #1

    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
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • S Offline
            S Offline
            sliver_twist
            wrote on last edited by
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0
              • J Offline
                J Offline
                JulienMaille
                wrote on last edited by
                #7

                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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved