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. Signals & Slots stopped working

Signals & Slots stopped working

Scheduled Pinned Locked Moved Unsolved General and Desktop
signal & slotc++11
15 Posts 5 Posters 4.0k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have the following code which used to work:

        connect(ui->descr_TextEdit, &QTextEdit::textChanged, [this]()
        {
            description = getDescription ();
        });
    

    Now it gives me the following error messages:

    1. 'description' is not captured
    2. the lambda has no capture default
    3. QString 'description' declared here.

    How can I fix this?
    Thank you.

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @gabor53,

      Does your class still have a description member?

      Paul ColbyP 1 Reply Last reply
      0
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Yes I have it.

        1 Reply Last reply
        0
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          Can you show the actual compiler error output?

          G 1 Reply Last reply
          0
          • Paul ColbyP Paul Colby

            Can you show the actual compiler error output?

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

            Hi @Paul-Colby
            Here is the complete error message:

            C:\Programming\Projects\Folkfriends_1_0\additem.cpp:-1: In lambda function:
            C:\Programming\Projects\Folkfriends_1_0\additem.cpp:254: error: 'description' is not captured
            description = getDescription ();
            ^
            C:\Programming\Projects\Folkfriends_1_0\additem.cpp:252: the lambda has no capture-default
            connect(ui->descr_TextEdit, &QTextEdit::textChanged, this
            ^
            C:\Programming\Projects\Folkfriends_1_0\additem.cpp:29: 'QString description' declared here
            void Additem::Addcontent(QString name, QString newWhat, QString newMaterial, QString newColor, QString description, QString newmonth)
            ^

            kshegunovK 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              replacing [this] with [&] works?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              G 1 Reply Last reply
              0
              • G gabor53

                Hi @Paul-Colby
                Here is the complete error message:

                C:\Programming\Projects\Folkfriends_1_0\additem.cpp:-1: In lambda function:
                C:\Programming\Projects\Folkfriends_1_0\additem.cpp:254: error: 'description' is not captured
                description = getDescription ();
                ^
                C:\Programming\Projects\Folkfriends_1_0\additem.cpp:252: the lambda has no capture-default
                connect(ui->descr_TextEdit, &QTextEdit::textChanged, this
                ^
                C:\Programming\Projects\Folkfriends_1_0\additem.cpp:29: 'QString description' declared here
                void Additem::Addcontent(QString name, QString newWhat, QString newMaterial, QString newColor, QString description, QString newmonth)
                ^

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

                @gabor53
                If description is a function parameter you must capture it explicitly:

                void Additem::Addcontent(QString name, QString newWhat, QString newMaterial, QString newColor, QString description, QString newmonth)
                {
                    // ... 
                    connect(ui->descr_TextEdit, &QTextEdit::textChanged, [this, description]()
                    {
                        description = getDescription();
                    }); 
                    // ...
                }
                

                But in any case such a capture is useless, because you can only do it by value.

                Read and abide by the Qt Code of Conduct

                VRoninV 1 Reply Last reply
                0
                • Paul ColbyP Paul Colby

                  Hi @gabor53,

                  Does your class still have a description member?

                  Paul ColbyP Offline
                  Paul ColbyP Offline
                  Paul Colby
                  wrote on last edited by
                  #8

                  @Paul-Colby said:

                  Does your class still have a description member?

                  @gabor53 said:

                  Yes I have it.

                  void Additem::Addcontent(QString name, QString newWhat, QString newMaterial, QString newColor, QString description, QString newmonth)
                  

                  That's not a class member.

                  Cheers.

                  1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @gabor53
                    If description is a function parameter you must capture it explicitly:

                    void Additem::Addcontent(QString name, QString newWhat, QString newMaterial, QString newColor, QString description, QString newmonth)
                    {
                        // ... 
                        connect(ui->descr_TextEdit, &QTextEdit::textChanged, [this, description]()
                        {
                            description = getDescription();
                        }); 
                        // ...
                    }
                    

                    But in any case such a capture is useless, because you can only do it by value.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @kshegunov
                    From the compiler output Additem::Addcontent is at line 29 while the lambda is at line 252 so I doubt it's nested. It's just description missing as a class member full stop

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    kshegunovK 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @kshegunov
                      From the compiler output Additem::Addcontent is at line 29 while the lambda is at line 252 so I doubt it's nested. It's just description missing as a class member full stop

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

                      @VRonin

                      so I doubt it's nested

                      At this point we can guess and bet.

                      It's just description missing as a class member full stop

                      Perhaps, or maybe the function is spanning 300 lines. The point is, without a more complete piece of the code we can't know for sure.

                      @gabor53

                      Do you mind sharing your class declaration and Additem::Addcontent function?

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • VRoninV VRonin

                        replacing [this] with [&] works?

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

                        @VRonin
                        Hi,
                        replacing [this] with [&] worked. Thank you.

                        kshegunovK 1 Reply Last reply
                        0
                        • G gabor53

                          @VRonin
                          Hi,
                          replacing [this] with [&] worked. Thank you.

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

                          @gabor53
                          Perhaps it compiles, but have you tested if it runs okay? Judging by the compile error and the solution, I'd be really surprised if you don't get a segfault. My previous request - to provide the whole function definition - stands.

                          Read and abide by the Qt Code of Conduct

                          G 1 Reply Last reply
                          0
                          • kshegunovK kshegunov

                            @gabor53
                            Perhaps it compiles, but have you tested if it runs okay? Judging by the compile error and the solution, I'd be really surprised if you don't get a segfault. My previous request - to provide the whole function definition - stands.

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

                            Hi @kshegunov
                            Here is the full code:
                            additem.h
                            Addcontent function
                            Thank you for your help.

                            kshegunovK 1 Reply Last reply
                            1
                            • G gabor53

                              Hi @kshegunov
                              Here is the full code:
                              additem.h
                              Addcontent function
                              Thank you for your help.

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

                              @gabor53
                              You are hiding the class member with the function argument because they have the same name. I advise not to use the same name for local and class variables.

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              1
                              • jerome_isAviableJ Offline
                                jerome_isAviableJ Offline
                                jerome_isAviable
                                wrote on last edited by
                                #15
                                1. 'description' is not captured
                                  said that in your lambda, 'description' has not been captured... then add it on the list of captured elements: [this, captured]
                                  I red inside the answers that you may declare same name for function and variable... don't do that, it will generate problems for sure.
                                2. QString 'description' declared here
                                  it is a clue for said that the answer of call this.getDescription() should be a QString (QString getDescription() const;)
                                  so 'description' has to be a QString (i think in private area of this). because you are not pass it, it is an undeclared new object inside your lambda function.

                                so give a varaible name unik and pass the variable inside your lambda by the array list [....] should fix your problem.

                                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