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. Function with Signals & Slots
Forum Updated to NodeBB v4.3 + New Features

Function with Signals & Slots

Scheduled Pinned Locked Moved Solved General and Desktop
functionsignal & slot
38 Posts 6 Posters 9.8k Views 4 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.
  • G gabor53

    @mrjj
    How can I find out if it supports lambdas?

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #19

    @gabor53
    well it can then compile the line.
    what compiler is it ?
    VS or mingw?
    and what version?

    G 1 Reply Last reply
    0
    • mrjjM mrjj

      @gabor53
      well it can then compile the line.
      what compiler is it ?
      VS or mingw?
      and what version?

      G Offline
      G Offline
      gabor53
      wrote on last edited by
      #20

      @mrjj
      I have mingw 4.9.2 32 bit

      mrjjM 1 Reply Last reply
      0
      • G gabor53

        @mrjj
        I have mingw 4.9.2 32 bit

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #21

        @gabor53
        ahh, u might need
        CONFIG+=c++11
        in your .pro file for it to use c++ 11 :)

        G 1 Reply Last reply
        0
        • mrjjM mrjj

          @gabor53
          ahh, u might need
          CONFIG+=c++11
          in your .pro file for it to use c++ 11 :)

          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #22

          @mrjj
          I have that too, so I guess it supports lambdas.

          kshegunovK 1 Reply Last reply
          0
          • G gabor53

            @mrjj
            I have that too, so I guess it supports lambdas.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #23

            @gabor53
            It's not the lambda.

            imagePath = findimage(QString);

            This doesn't make sense neither to the compiler nor to me, so you should fix it. What argument are you trying to use findimage with? Currently it has none, and instead of an argument a type name is provided.

            Kind regards.

            Read and abide by the Qt Code of Conduct

            mrjjM 1 Reply Last reply
            2
            • kshegunovK kshegunov

              @gabor53
              It's not the lambda.

              imagePath = findimage(QString);

              This doesn't make sense neither to the compiler nor to me, so you should fix it. What argument are you trying to use findimage with? Currently it has none, and instead of an argument a type name is provided.

              Kind regards.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #24

              @kshegunov
              ahh good spotted. thats the void = QString :)
              I completely missed that .)

              kshegunovK 1 Reply Last reply
              0
              • mrjjM mrjj

                @kshegunov
                ahh good spotted. thats the void = QString :)
                I completely missed that .)

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #25

                @mrjj
                I just read the compiler errors, it says it right there:

                expected primary-expression before ')' token

                ;)

                Read and abide by the Qt Code of Conduct

                mrjjM 1 Reply Last reply
                0
                • kshegunovK kshegunov

                  @mrjj
                  I just read the compiler errors, it says it right there:

                  expected primary-expression before ')' token

                  ;)

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #26

                  @kshegunov
                  yeah it's a meaningful and good compiler error :)

                  1 Reply Last reply
                  1
                  • L Offline
                    L Offline
                    Lineaxe
                    wrote on last edited by
                    #27

                    Heheh, of course that's right. I gotta get back to programming again, I been installing , upgrading and reading too much...
                    ** it doesn't matter where fileName (or m_fileName) is declared, after the connect(...) call it will not be set yet because the findimage(bool) slot was not yet called. It will be called when the user clicks on the button - and you never know when the user will click the button. **

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gabor53
                      wrote on last edited by
                      #28

                      I changed the following line:

                             connect(Image_Button, &QPushButton::clicked, [this]() { imagePath =  findimage( QString);});
                      
                      to 
                      
                         connect(Image_Button, &QPushButton::clicked, [this]() { imagePath =  findimage( fileName);});
                      
                      Now I get the following error message:
                      C:\Programming\backup\Folkfriends\additem.cpp:195: error: no match for 'operator=' (operand types are 'QString' and 'void')
                              connect(Image_Button, &QPushButton::clicked, [this]() { imagePath =  findimage( fileName);});
                                                                          
                      What else to change? Thank you.
                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #29

                        Hi
                        in the first post you show it as
                        findimage(bool)

                        so that would be callable as
                        imagePath = findimage( true);

                        So what you give it as parameter, depends on how u declare it.
                        (in the .h file)

                        G 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          Hi
                          in the first post you show it as
                          findimage(bool)

                          so that would be callable as
                          imagePath = findimage( true);

                          So what you give it as parameter, depends on how u declare it.
                          (in the .h file)

                          G Offline
                          G Offline
                          gabor53
                          wrote on last edited by
                          #30

                          @mrjj
                          I changed it from findimage(bool) to findimage(fileName) because I need it to return the fileName.
                          In the .h file it is defined as void findimage(QString fileName);
                          I still have the same error message. Thank you.

                          mrjjM 1 Reply Last reply
                          0
                          • G gabor53

                            @mrjj
                            I changed it from findimage(bool) to findimage(fileName) because I need it to return the fileName.
                            In the .h file it is defined as void findimage(QString fileName);
                            I still have the same error message. Thank you.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #31

                            @gabor53 said:
                            hi
                            I would kinda expect it to be declared as
                            void findimage();
                            as you
                            return(fileName);
                            in the function and return filename that way.
                            so why you need a QString as parameter too ?

                            G 2 Replies Last reply
                            1
                            • mrjjM mrjj

                              @gabor53 said:
                              hi
                              I would kinda expect it to be declared as
                              void findimage();
                              as you
                              return(fileName);
                              in the function and return filename that way.
                              so why you need a QString as parameter too ?

                              G Offline
                              G Offline
                              gabor53
                              wrote on last edited by
                              #32

                              @mrjj
                              I changed it as you recommended:
                              in the .h file: void findimage();

                               connect(Image_Button, &QPushButton::clicked, [this]() { imagePath =  findimage();});
                              
                              the function : void Additem::findimage();
                              It still gives the same exact error message. Thnks.
                              
                              
                              kshegunovK 1 Reply Last reply
                              0
                              • G gabor53

                                @mrjj
                                I changed it as you recommended:
                                in the .h file: void findimage();

                                 connect(Image_Button, &QPushButton::clicked, [this]() { imagePath =  findimage();});
                                
                                the function : void Additem::findimage();
                                It still gives the same exact error message. Thnks.
                                
                                
                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by
                                #33

                                @gabor53
                                void means "nothing", so you're trying to save that "nothing" into a variable, how should that happen? The compiler doesn't know, I don't and I'm pretty sure no one does. If you want to assign a value returned from a function to a variable, well then, by all means, make the function return the proper type and value. Meaning, if you want the function to return a QString then you declare it as such:

                                class MyClass
                                {
                                    QString myFunction(); //< MyClass::myFunction is going to return a value of type QString.
                                }
                                
                                QString MyClass::myFunction()
                                {
                                    return QString("Some string"); //< The function returns the value and it is of type QString
                                }
                                

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                1
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #34

                                  sorry my bad
                                  void findimage();

                                  should have been

                                  QString findimage();

                                  I think i should let @kshegunov handle this one as I keep missing the errors :)

                                  kshegunovK 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @gabor53 said:
                                    hi
                                    I would kinda expect it to be declared as
                                    void findimage();
                                    as you
                                    return(fileName);
                                    in the function and return filename that way.
                                    so why you need a QString as parameter too ?

                                    G Offline
                                    G Offline
                                    gabor53
                                    wrote on last edited by
                                    #35

                                    @mrjj
                                    I changed the .h file declaration to QString findimage();.
                                    Now it works without error, but I still can't capture the findimage returned value (fileName) in the line after connect.
                                    code
                                    The function works because the qDebug in the last line returns the correct value.
                                    How can I return the value before the add layout section so I would be able to display the image next to the button? (That was the original issue I had).

                                    Thank you.

                                    kshegunovK 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      sorry my bad
                                      void findimage();

                                      should have been

                                      QString findimage();

                                      I think i should let @kshegunov handle this one as I keep missing the errors :)

                                      kshegunovK Offline
                                      kshegunovK Offline
                                      kshegunov
                                      Moderators
                                      wrote on last edited by
                                      #36

                                      @mrjj
                                      As I noted before, the errors are swiftly and cleanly reported by the compiler:

                                      C:\Programming\backup\Folkfriends\additem.cpp:195: error: no match for 'operator=' (operand types are 'QString' and 'void')
                                      connect(Image_Button, &QPushButton::clicked, this { imagePath = findimage( fileName);});

                                      So the compiler can't make heads or tails of what to do with the arguments given to the assignment operator. :)

                                      Read and abide by the Qt Code of Conduct

                                      1 Reply Last reply
                                      0
                                      • G gabor53

                                        @mrjj
                                        I changed the .h file declaration to QString findimage();.
                                        Now it works without error, but I still can't capture the findimage returned value (fileName) in the line after connect.
                                        code
                                        The function works because the qDebug in the last line returns the correct value.
                                        How can I return the value before the add layout section so I would be able to display the image next to the button? (That was the original issue I had).

                                        Thank you.

                                        kshegunovK Offline
                                        kshegunovK Offline
                                        kshegunov
                                        Moderators
                                        wrote on last edited by kshegunov
                                        #37

                                        @gabor53

                                        http://forum.qt.io/topic/64635/function-with-signals-slots/12

                                        @jsulm already gave you what the problem with this construct is. You're trying to predict the future. You want at some point when the user clicks a button to take an image, and that's fine. The erroneous part is when you want to go back in the past where the connect was made and do something with that image, it's simply not possible. If you want to operate on the value returned from your function, then you should put the code that uses it inside the lambda function, so you wouldn't need to jump through time-travel hoops to get the string. QObject::connect simply says that some function should be called when a signal is emitted, it doesn't guarantee you in any way when or if that shall happen.

                                        Kind regards.

                                        Read and abide by the Qt Code of Conduct

                                        1 Reply Last reply
                                        1
                                        • G Offline
                                          G Offline
                                          gabor53
                                          wrote on last edited by
                                          #38

                                          Thank you all. It worked.

                                          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