如何让光标位于文本编辑的开头?

问题描述 投票:0回答:1

我做了一个简单的搜索功能,但是只有当用户将光标移动到文本编辑的开头时它才有效。

我想知道是否有什么办法可以让光标自动出现在那里。

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);
}
c++ qt qtextedit qtextcursor
1个回答
6
投票

您快要得到结果了。

通过使用

QTextEdit::setTextCursor
,您可以将可见光标移动到您想要的位置:

QTextCursor textCursor = ui->textEdit->textCursor();
textCursor.movePosition(QTextCursor::Start);
ui->textEdit->setTextCursor(textCursor); // The line to add
© www.soinside.com 2019 - 2024. All rights reserved.