Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

list of qtextedit. How to get the one that has changed?



  • Hey,

    this one works absolutely fine:

    QTextEdit *textEdit ;
    
    void MainWindow::doStuff() {
        textEdit = new QTextEdit();
        connect(textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(texthaschanged()));
    }
    
    void MainWindow::texthaschanged() {
        QTextCursor myCursor;
        myCursor = textEdit ->textCursor();
        qDebug() << "text has changed at position" << myCursor.position();
    }
    

    But what to do when I have the following?

    QList<QTextEdit*> myList;
    

    How do I know now which cursor has changed?

    Thanks!



  • @LeoC
    You read up about C++ lambdas. :)

    void MainWindow::doStuff() {
        QTextEdit *textEdit = new QTextEdit();
        myList.append(textEdit);
        connect(textEdit, &QTextEdit::cursorPositionChanged,
                this, [this](QTextEdit *textEdit) { texthaschanged(textEdit);  } );
    }
    
    void MainWindow::texthaschanged(QTextEdit *textEdit) {
        ...
    }
    

    This passes the textEdit emitting the signal as a parameter to the texthaschanged(QTextEdit *textEdit) slot, so it uses that to access that one in the body of the method.

    We are no longer using the single member variable QTextEdit *textEdit ; you had. You get rid of that, and use your QList<QTextEdit*> myList to store pointers to the various QTextEdits.

    You also use this opportunity to start using the new style signals & slots syntax instead of the SIGNAL/SLOT() macros. You get better edit/compile-time support.

    EDIT @KroMignon has corrected my (untested!) code below :) The connect() statement should read:

        connect(textEdit, &QTextEdit::cursorPositionChanged,
                this, [this, textEdit]() { texthaschanged(textEdit);  } );
    


  • @JonB thanks for your answer.

    To be honest, I am not very familiar with lambdas. I just started reading about it and I think I got a basic understanding of it.

    Nevertheless I can't get your example working. I get the following error:

    error: static assertion failed: Signal and slot arguments are not compatible.
    

    Can you give ma a hint for this?



  • @LeoC said in list of qtextedit. How to get the one that has changed?:

    Can you give ma a hint for this?

    There is a little error in proposed code, QTextEdit::cursorPositionChanged() don't have arguments!
    You have to capture booth variable to use lambdas, like this:

    void MainWindow::doStuff() {
        QTextEdit *textEdit = new QTextEdit();
        myList.append(textEdit);
        connect(textEdit, &QTextEdit::cursorPositionChanged,
                this, [this, textEdit]() { texthaschanged(textEdit);  } );
    }
    

    I could suggest you to take a look at https://blog.feabhas.com/2014/03/demystifying-c-lambdas/ to better understand lambda functions usage in C++.
    And this for combining lambdas and Qt: https://medium.com/genymobile/how-c-lambda-expressions-can-improve-your-qt-code-8cd524f4ed9f



  • @KroMignon thanks. I guess I figured it out. This one works fine:

        for(int i_Ctr = 0; i_Ctr < 3; i_Ctr++) {
            QTextEdit *textEdit = new QTextEdit();
    
            connect(textEdit, &QTextEdit::cursorPositionChanged, [this, textEdit] {
                this->texthaschanged(textEdit);
            });
            textEdit->show();
        }
    

    In my previous example the definition of QTextEdit wasn't part of the function itself but rather part of the h-file. That was something I just didn't recognized.
    Anyway, now I can move on :) Thank you!


Log in to reply