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 and Slots
Forum Updated to NodeBB v4.3 + New Features

Signals and Slots

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 2.8k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have the following code:

        connect(ui->notes_TextEdit, &QTextEdit::textChanged, [&]()
        {
            notes = getNotes ();
        });
    

    Get notes is defined as

      QString getNotes();
    
    

    in .h.
    The function:

    QString Additem::getNotes()
    {
        notes = ui->notes_TextEdit->toPlainText ();
        notes = notes.simplified ();
    
        return notes;
    }
    

    Whenever the program execution reaches connect.... the program crashes. The same code works with other variables elsewhere. What is wrong with this one?
    Thank you.

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, I think in the lambda you're refernce capturing your AddItem instance, try changing to value capture:

      connect(ui->notes_TextEdit, &QTextEdit::textChanged, [this]()
      {
          notes = getNotes ();
      });
      
      G 1 Reply Last reply
      0
      • hskoglundH hskoglund

        Hi, I think in the lambda you're refernce capturing your AddItem instance, try changing to value capture:

        connect(ui->notes_TextEdit, &QTextEdit::textChanged, [this]()
        {
            notes = getNotes ();
        });
        
        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @hskoglund
        I tried and I got an error message:
        C:\Programming\Projects\Folkfriends_1_0\additem.cpp:342: error: 'notes' is not captured
        notes = getNotes ();
        ^

        1 Reply Last reply
        0
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          Ok, try:

          connect(ui->notes_TextEdit, &QTextEdit::textChanged, [this,&]()
          {
              notes = getNotes ();
          });
          
          G 1 Reply Last reply
          0
          • hskoglundH hskoglund

            Ok, try:

            connect(ui->notes_TextEdit, &QTextEdit::textChanged, [this,&]()
            {
                notes = getNotes ();
            });
            
            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            @hskoglund
            Thanks but it generated another error message:

            C:\Programming\Projects\Folkfriends_1_0\additem.cpp:340: error: expected identifier before ']' token
            connect(ui->notes_TextEdit, &QTextEdit::textChanged, this,&
            ^
            C:\Programming\Projects\Folkfriends_1_0\additem.cpp:342: error: 'notes' is not captured
            notes = getNotes ();
            ^
            and the code:

                connect(ui->notes_TextEdit, &QTextEdit::textChanged, [this,&]()
                {
                    notes = getNotes ();
                });
            
            1 Reply Last reply
            0
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              Sorry it's tricky with the capture list :-) Try again:

              connect(ui->notes_TextEdit, &QTextEdit::textChanged, [=,&notes]()
              {
                  notes = getNotes ();
              });
              
              G 1 Reply Last reply
              0
              • hskoglundH hskoglund

                Sorry it's tricky with the capture list :-) Try again:

                connect(ui->notes_TextEdit, &QTextEdit::textChanged, [=,&notes]()
                {
                    notes = getNotes ();
                });
                
                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

                @hskoglund
                Now we went further:
                I received the following error message:

                0_1473520529168_Capture.JPG

                It happens in a file called atomic_base.h (frankly, I have no idea what that is) .
                Here is the file:
                atomic_base.h.
                The error occurs at line 396.

                1 Reply Last reply
                0
                • hskoglundH Offline
                  hskoglundH Offline
                  hskoglund
                  wrote on last edited by
                  #8

                  Hi, I think it's complaining that you're reusing the same variable notes in both the lambda and in getNotes(), try change getNodes something like this:

                  QString Additem::getNotes()
                  {
                      QString notesTemp = ui->notes_TextEdit->toPlainText ();
                      notesTemp = notesTemp.simplified ();
                  
                      return notesTemp;
                  }
                  
                  G 1 Reply Last reply
                  0
                  • hskoglundH hskoglund

                    Hi, I think it's complaining that you're reusing the same variable notes in both the lambda and in getNotes(), try change getNodes something like this:

                    QString Additem::getNotes()
                    {
                        QString notesTemp = ui->notes_TextEdit->toPlainText ();
                        notesTemp = notesTemp.simplified ();
                    
                        return notesTemp;
                    }
                    
                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    Same error message in atomic_base.h.

                    1 Reply Last reply
                    0
                    • hskoglundH Offline
                      hskoglundH Offline
                      hskoglund
                      wrote on last edited by
                      #10

                      Hmm, that notes QString variable, it's not in your Additem class, is it declared in some other class?

                      G 1 Reply Last reply
                      0
                      • hskoglundH hskoglund

                        Hmm, that notes QString variable, it's not in your Additem class, is it declared in some other class?

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

                        @hskoglund
                        It is declared in Additem.

                        1 Reply Last reply
                        0
                        • hskoglundH Offline
                          hskoglundH Offline
                          hskoglund
                          wrote on last edited by
                          #12

                          Ok, I think it's colliding with same other variable notes that you have in your Additem class, most likely a function argument, for example in the function that has that connect statement.

                          Anyway, suggest to refactor it to same other name, not notes, then you also should be able to use [this], something like this:

                          connect(ui->notes_TextEdit, &QTextEdit::textChanged, [this]()
                          {
                              anythingButNotes = getNotes ();
                          });
                          
                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gabor53
                            wrote on last edited by
                            #13

                            This worked (no idea why):

                                connect(ui->notes_TextEdit, &QTextEdit::textChanged, [this]()
                                {
                                   QString notes = getNotes ();
                                });
                            

                            notes is already defined in Additem.h....
                            Thank you for all your help.

                            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