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. [solved] connect to slot and specifying argument values
Forum Updated to NodeBB v4.3 + New Features

[solved] connect to slot and specifying argument values

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.2k 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.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    I have a QLineEdit object and I have connected the returnPressed() signal to a slot. The same slot though is called for another signal, and I'd like to differentiate between the slot caller using a bool argument. Unfortunately, I've been unable to call a slot and specify either true or false. I've tried like this:
    @
    (...)
    slots:
    onEditFinished(bool b);
    (...)
    connect(pEditObj, SIGNAL(returnPressed()), this, SLOT(onEditFinished(bool b=true)));
    connect(pEditObj, SIGNAL(returnPressed()), this, SLOT(onEditFinished(true)));@
    The first connect I've used seems to compile fine with no errors or warnings, but at runtime the slot function onEditFinished() is not called at all. The second connect I've used would not compile since the connection requires a function prototype, not a real function call.

    Question is, is it possible to connect to a slot and spcify a slot argument value? I know this is possible when the incoming signal has parameters, and they will be passed through to the slot function, but in this case the returnPressed() signal has no parameters.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      No, you can't. You could use a lambda if c++11 is an option.

      Why do you have a slot call onEditFinished with a bool parameter ?

      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
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #3

        Tank you for your answer ;)
        I have two objects (a few more coming as well) that should call the same onEditFinished slot because I have to do the same tasks; for instance I have a QPushButton that resets to the default value and a QLineEdit that should set a new one with its contents. After finishing they should do the same tasks, but I want to hide the QLineEdit if the call was from there. So basically I thought onEditFinished(true) means it comes from the QLineEdit object, onEditFinished(false) comes from wherever else.

        Luckily, I just found the "QObject::sender()":http://qt-project.org/doc/qt-4.8/qobject.html#sender method in the docs and looks like this is something I could use. I never used it so I'm expecting trouble, but it should be fine for the task.

        p.s. yes, C++11 is an option , could you show me the way you were thinking about?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          sender() is the right and it works. You can set objectname using setObjectName to differentiate those objects in slot.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Do you mean hide the QLineEdit once it emitted returnPressed ?

            If so, just connect returnPressed on hide and keep your slot clean

            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
            • T3STYT Offline
              T3STYT Offline
              T3STY
              wrote on last edited by
              #6

              No, the slot actually does more things than hiding the QLineEdit object; if it was just about hiding it would have been too simple, wouldn't it? :-)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Indeed, but one never knows :-)

                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

                • Login

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