将 QTextCursor 移动到末尾时出现问题
我正在尝试在我正在编写的编辑器中实现简单的文本搜索。一切都很好,直到出现这个问题!我正在尝试在这里实现向后搜索。其过程是:向后查找主题,如果没有找到,则嘟嘟一声,如果再次按下查找按钮,则转到文档末尾,重新进行查找。 “reachedEnd”是一个 int,定义为编辑器类的私有成员。这是执行向后搜索的函数。
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,表示失败。这怎么会失败呢?!!提前致谢。
I'm trying to implement a simple text search in an editor i'm writing. Everything have been fine until this problem! I'm trying to implement a backward search here. The procedure is: look for the subject backward, if not found, beep once, and if find button was pressed again, go to the end of the document, and do the search again. "reachedEnd" is an int, defined as a private member of the editor class. Here's the function that does the backward search.
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();
}
}
}
The problem is that cursor doesn't move to the end! And it returns False, which means failure. How can this fail?!! Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于这个问题得到了一些看法,而且似乎是一个常见问题,我认为它值得一个答案(尽管作者肯定已经弄清楚了)。
从文档中:
因此,您获得了它的副本,并且通过执行
cursor.movePosition(QTextCursor::End);
它不起作用。我所做的是:
Since this question got some views and it appears to be a common problem, I think it deserves an answer (even though the author most surely figured it out).
From the documentation:
So you got a copy of it and by doing
cursor.movePosition(QTextCursor::End);
it wouldn't work.What I did is:
如果我像这样简化您的代码:
...我看到您在cursor.isNull() 条件为真时调用了movePosition() 函数。
也许这就是它不起作用的原因......
If I simplify your code like this:
...I see that you call the movePosition() function while the cursor.isNull() condition is true.
Maybe this is the reason it doesn't work...