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

move QTextCursor



  • Hi, everyone.
    I'm trying to move a QTextCursor in a specific position. I'm trying with the following code, but i'm not obtaining results:

        QTextCursor curs=ui->textEdit->textCursor();
        curs.movePosition(QTextCursor::Start);
        ui->textEdit->setTextCursor(curs);
    

    With this solution, the cursor simply disappears from my textEdit.
    Can you help me?


  • Lifetime Qt Champion

    Hi,

    What version of Qt ?
    On what OS ?
    Please provide a minimal compilable example that reproduce this behaviour.


  • Lifetime Qt Champion

    Hi
    QDebug is your friend :)

       QTextCursor curs = ui->textEdit->textCursor();
        qDebug() << "now:" << curs.position();
        curs.movePosition(QTextCursor::Start);
        ui->textEdit->setTextCursor(curs);
        qDebug() << "after:" << curs.position();
    

    It goes to position 0

    The cursor is not vanishing by an error. ( i'm guessing here..)
    I think you use a button or something like that to test the code.
    Then the button will then take focus (when you click it) and hence the cursor is not shown since
    the TextEdit no longer has focus. the button has.

    set button to NoFocus and see.
    alt text

    alt text



  • @mrjj Thanks so much


  • Lifetime Qt Champion

    @Martinuccia_96
    Well it also fooled me for a moment :)
    Then i recalled that buttons takes focus too so they can activate with keyboard.


Log in to reply