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. What would be object name which sends event (Solved)

What would be object name which sends event (Solved)

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 13.3k 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.
  • Y Offline
    Y Offline
    Yash
    wrote on last edited by
    #1

    When we hit a button , it will call slot accordingly. Now I want to know the objectname of that button in the slot.

    These are dynamics buttons with custom names.

    http://kineticwing.com : Web IDE, QSS Editor
    http://speedovation.com : Development Lab

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      you can use QObject::sender() to get the sending object. There you can query for the object name:

      @
      void myslot()
      {
      QObject* pObject = sender();
      QString name = pObject->objectName();
      }
      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        Use QObject::sender() inside your slot to get a pointer to the emitter object.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Be aware, that the object returned by sender() is only valid if the slot is called via signal/slot connection (using QObject::connect() method). When the slot is called directly, sender() returns a null pointer.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            Yash
            wrote on last edited by
            #5

            Thank you Gerolf Reinwardt

            Yes Volker it's via signal/slot

            http://kineticwing.com : Web IDE, QSS Editor
            http://speedovation.com : Development Lab

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Good. Nontheless, I'd put a check if(pObject) { ... } around the code, just to be on the save side.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                Yash
                wrote on last edited by
                #7

                Ok nice. Now I'll also :)

                http://kineticwing.com : Web IDE, QSS Editor
                http://speedovation.com : Development Lab

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  I wanted to keep it as small as possible, but I would also do it :-)

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    To save you a line :-) you can write this:

                    @
                    void myslot()
                    {
                        if(QObject* pObject = sender()) {
                        QString name = pObject->objectName();
                    }
                    }
                    @

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tobias.hunger
                      wrote on last edited by
                      #10

                      QSignalMapper might be an option for you, too.

                      1 Reply Last reply
                      0
                      • Y Offline
                        Y Offline
                        Yash
                        wrote on last edited by
                        #11

                        It's rocking guys
                        Nice one Volker :)

                        [quote author="Gerolf Reinwardt" date="1292514405"]I wanted to keep it as small as possible, but I would also do it :-)[/quote]

                        Totally agreed with you :)

                        http://kineticwing.com : Web IDE, QSS Editor
                        http://speedovation.com : Development Lab

                        1 Reply Last reply
                        0
                        • Y Offline
                          Y Offline
                          Yash
                          wrote on last edited by
                          #12

                          [quote author="Tobias Hunger" date="1292515404"]QSignalMapper might be an option for you, too.[/quote]
                          Let me check this out.

                          http://kineticwing.com : Web IDE, QSS Editor
                          http://speedovation.com : Development Lab

                          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