c++qtqtexteditqtextcursor

How do you get the cursor at the beginning of a textedit?


I made a simple search function, however it only works if the user moves the cursor to beginning of the textedit.

I was wondering if there was anything I can do to make the cursor automatically appear there.

void Dialog::on_pushButton_clicked()
{
    QString month;
    QString day;
    QString year;

    month=ui->comboBox->currentText();
    day=ui->comboBox_2->currentText();
    year=ui->comboBox_3->currentText();

    QTextCursor textCursor = ui->textEdit->textCursor();
    textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1);

    QString date= month + "/" + day + "/" + year;
    qDebug() << date;
    ui->textEdit->find(date, QTextDocument::FindWholeWords);
}

Solution

  • You were almost to the result.

    By using QTextEdit::setTextCursor, you can move the visible cursor where you want:

    QTextCursor textCursor = ui->textEdit->textCursor();
    textCursor.movePosition(QTextCursor::Start);
    ui->textEdit->setTextCursor(textCursor); // The line to add