qt4 - 将 QTextCursor 移动到末尾的问题

标签 qt4 qtextdocument

我正在尝试在我正在编写的编辑器中实现一个简单的文本搜索。一切都很好,直到出现这个问题!我正在尝试在这里实现向后搜索。过程是:向后查找主题,如果没有找到,哔一声,如果再次按下查找按钮,则转到文档末尾,重新搜索。 "reachedEnd"是一个 int,定义为编辑器类的私有(private)成员。这是进行向后搜索的函数。

void TextEditor::findPrevPressed() {
    QTextDocument *document = curTextPage()->document();
    QTextCursor    cursor   = curTextPage()->textCursor();

    QString find=findInput->text(), replace=replaceInput->text();


    if (!cursor.isNull()) {
        curTextPage()->setTextCursor(cursor);
        reachedEnd = 0;
    }
    else {
        if(!reachedEnd) {
            QApplication::beep();
            reachedEnd = 1;
        }
        else {
            reachedEnd = 0;
            cursor.movePosition(QTextCursor::End);
            curTextPage()->setTextCursor(cursor);
            findPrevPressed();
        }
    }
}

问题是光标没有移动到最后!它返回 False,这意味着失败。这怎么可能失败?!!提前致谢。

最佳答案

由于这个问题得到了一些看法,而且它似乎是一个普遍的问题,我认为它值得一个答案(尽管作者最肯定地想通了)。

从文档中:

QTextCursor QPlainTextEdit::textCursor() const
Returns a copy of the QTextCursor that represents the currently visible cursor. Note that changes on the returned cursor do not affect QPlainTextEdit's cursor; use setTextCursor() to update the visible cursor.



所以你得到了它的副本并通过执行cursor.movePosition(QTextCursor::End);它行不通。

我所做的是:
QTextCursor newCursor = new QTextCursor(document);
newCursor.movePosition(QTextCursor::End);
curTextPage()->setTextCursor(newCursor);

关于qt4 - 将 QTextCursor 移动到末尾的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6786641/

相关文章:

c++ - QVariantMap DBusMenuExporterDBus GLib-GObject-CRITICAL GLib-GObject-警告

c++ - 自定义 QWIzard 中的按钮?

c++ - 如何访问 QTextDocument 的 QUndoStack?

c++ - Qt4/PyQt4 - 无法为 QTextDocument 设置默认字体

c++ - Qt编译错误1

c++ - 如何使用 QT 打印 prn 文件?

c++ - 调用父控件函数

qt - 在 QTextEdit 中绘画而不影响文本

html - QTextDocument HTML 表格宽度