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. Passing another variable to a SLOT in QObject::connect
Forum Updated to NodeBB v4.3 + New Features

Passing another variable to a SLOT in QObject::connect

Scheduled Pinned Locked Moved General and Desktop
15 Posts 8 Posters 19.5k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    Franzk
    wrote on last edited by
    #6

    [quote author="peppe" date="1297240228"]In your (very simple) case, if it's applicable, you can just provide a default argument for the slot. Otherwise, simply create another slot that calls the setText one with the string you want.[/quote]Indeed. With a slot called setText(), providing a default argument can be misleading:

    @myLabel->setText();@

    What does that do?

    Oh well. It's a style issue.

    "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      manasij7479
      wrote on last edited by
      #7

      bq. Otherwise, simply create another slot that calls the setText one with the string you want.

      Any way without creating whole new classes?

      "Error, no keyboard — press F1 to continue."

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

        "QSignalMapper":http://doc.qt.nokia.com/4.7/qsignalmapper.html might also be an option.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #9

          to solve simple problem like yours I use this solution:
          @
          connect(button1, SIGNAL(clicked()), SLOT(buttonClicked());
          connect(button2, SIGNAL(clicked()), SLOT(buttonClicked());
          button1->setProperty("name", "button1");
          button2->setProperty("name", "button2");
          ...
          ...
          MyClass::buttonClicked()
          {
          QPushButton *button = sender();
          if(button->property("name").toString() == "button1")
          {
          .....
          .....
          }
          else if(button->property("name").toString() == "button2")
          {
          ....
          ....
          }
          }
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #10

            Luca, that is unsave code.

            You don't know what kind of object sender() refers to, or if it is non-0 at all (direct call to buttonClicked()). So, you should at least check that before assuming this. Using a QSignalMapper is the safe way to go, and it still allows other ways to trigger the slot and still work correctly.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #11

              Safe or not safe, it's arguably not even simpler than the QSignalMapper approach. Likewise, if a slot is going to make (significant) behavioral changes based on the sender(), then it is probably time to review the design anyway.

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca
                wrote on last edited by
                #12

                [quote author="Andre" date="1297288071"]Luca, that is unsave code.

                You don't know what kind of object sender() refers to, or if it is non-0 at all (direct call to buttonClicked()). So, you should at least check that before assuming this. Using a QSignalMapper is the safe way to go, and it still allows other ways to trigger the slot and still work correctly.
                [/quote]
                Yes, I usually check if pointer is a push button with a dynamic_cast:
                @
                QPushButton button = dynamic_cast<QPushButton>(sender());
                if(button==NULL)
                {
                return;
                }
                @

                Mine is only a fast example...

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Franzk
                  wrote on last edited by
                  #13

                  qobject_cast might be more thorough.

                  "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MarekR22
                    wrote on last edited by
                    #14

                    Hi there is a tool which do exactly what you need.
                    See "QSignalMapper":http://doc.trolltech.com/latest/qsignalmapper.html
                    There is good example how to use it.

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

                      If you're in the same class (or have access to the possible sender objects pointers) you simply can compare pointers:

                      @
                      void MyClass::buttonClicked()
                      {
                      if(sender() == button1) {
                      // ....
                      } else if(sender() == button2) {
                      // ....
                      }
                      }
                      @

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

                      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