Signals and Slots



  • 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.



  • 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 ();
    });
    


  • @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 ();
    ^



  • Ok, try:

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


  • @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 ();
        });
    


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

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


  • @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.



  • 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;
    }
    


  • Same error message in atomic_base.h.



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



  • @hskoglund
    It is declared in Additem.



  • 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 ();
    });
    


  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.