简体   繁体   English

Qt C ++如何在textedit的开头获取光标?

[英]Qt C++ How do you get the cursor at the beginning of a textedit?

Basically I made a simple search function, however it only works if the user moves the cursor to beginning of the textedit. 基本上,我做了一个简单的搜索功能,但是只有当用户将光标移到textedit的开头时它才起作用。 I was wondering if there was anything I can do to make the cursor automatically appear there. 我想知道是否有什么办法可以使光标自动出现在这里。 Any input would be greatly appreciated. 任何投入将不胜感激。

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);

}

You were almost to the result. 你几乎要结果了。

By using QTextEdit::setTextCursor , you can move the visible cursor where you want: 通过使用QTextEdit::setTextCursor ,可以将可见光标移动到所需位置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM