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] Sending a argument to a slot
QtWS25 Last Chance

[SOLVED] Sending a argument to a slot

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

    I understand that i am not able to pass a argument to a slot that was not a argument of the signal.
    However this is exactly what i want to be able to achieve. and im sure there must be a work around if anyone can help?

    basically when a button is pressed i want to send some text through to my slot function:
    e.g
    @QObject::connect(button, SIGNAL(clicked()),this, SLOT(updateL("C:/")));@

    regards, Tim.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kevina67
      wrote on last edited by
      #2

      You can use C++11 lambdas :

      @
      QObject::connect(button, &QPushButton::clicked, [this] () {
      updateL("C:/")
      });
      @

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

        Or you use a [[doc:QSignalMapper]] object, especially if you're still on Qt4 or using a compiler that doesn't understand C++/11

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          Simply create a slot function without argument, connect your signal to this slot, in which your original slot get called as a normal function.

          If you do not want to create a named function and your are using Qt5, you can connect your signal to the lambda function, in which you can call your original slot. [as kevlna65 suggested]

          [quote author="Timmoth" date="1375433317"]I understand that i am not able to pass a argument to a slot that was not a argument of the signal.
          However this is exactly what i want to be able to achieve. and im sure there must be a work around if anyone can help?

          basically when a button is pressed i want to send some text through to my slot function:
          e.g
          @QObject::connect(button, SIGNAL(clicked()),this, SLOT(updateL("C:/")));@

          regards, Tim.[/quote]

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Timmoth
            wrote on last edited by
            #5

            Thanks for both replys i think im going to go with Kevlna's method though since i am using Qt5.
            @ QObject::connect(button, &QPushButton::clicked, this{Hi("C:/");});@
            worked perfectly!

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Timmoth
              wrote on last edited by
              #6

              However i have an issue now, when i try to pass a Qstring through i get an error:

              error: C3493: 'path' cannot be implicitly captured because no default capture mode has been specified
              @
              QString path = d.absoluteFilePath();
              QObject::connect(button, &QPushButton::clicked, this{Hi(path);});
              @
              regards, Tim

              Edit: please use @ tags around code sections; Andre

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kevina67
                wrote on last edited by
                #7

                Use this instead :

                @
                QObject::connect(button, &QPushButton::clicked, [this] () {

                // You're inside the lambda, you can write almoste everything here.    
                QString path = d.absoluteFilePath();    
                Hi(path);
                

                });@

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Timmoth
                  wrote on last edited by
                  #8

                  i understand it a bit better now thanks Kevlna but im still getting the error message:

                  error: C3493: ‘path’ cannot be implicitly captured because no default capture mode has been specified

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    PeerS
                    wrote on last edited by
                    #9

                    As d is a local variable you have tell the lambda how to capture it:

                    @
                    (1) auto l = &d { // only d is captured by reference };
                    (2) auto l = d { // only d is captured by value };
                    (3) auto l = & { // all local variables are captured by reference. };
                    (4) auto l = = { // all local variables are captured by value. (A copy will be made!) };
                    @

                    There are several ways to combine different capture methods, refer to a c++ book of your choice for more details. (E.g. The C++ Programming Language 4, 11.4.3, page 293).

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      Timmoth
                      wrote on last edited by
                      #10

                      ok thanks alot PeerS! ill look into capture methods.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Timmoth
                        wrote on last edited by
                        #11

                        I cannot seem to get this to work! ive looked up a few examples to no success.

                        @
                        auto func1 = & { return d.absoluteFilePath(); };
                        QObject::connect(button, &QPushButton::clicked, this {
                        Hi(func1);
                        });
                        @
                        am i using this wrongly?
                        regards, Tim.

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          Timmoth
                          wrote on last edited by
                          #12

                          Ok - finally i have gotten it to work.

                          @
                          auto func1 = = { return d.absoluteFilePath(); };
                          QObject::connect(button, &QPushButton::clicked, = {
                          Hi(func1());
                          });
                          @

                          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