在 PyQt5 文本编辑中移动光标不起作用 [英] Moving the cursor in a PyQt5 text edit doesn't work

查看:154
本文介绍了在 PyQt5 文本编辑中移动光标不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Frescboaldi 做出贡献,这是一个 PyQt5 应用程序,并在与核心文本编辑交互时遇到问题组件.

I'm contributing to Frescboaldi, a PyQt5 application and experience problems interacting with the core text edit component.

似乎无论我尝试什么,我都无法让 setPositionmovePosition 工作.

It seems whatever I try I can't get either of setPosition or movePosition to work.

代码

cursor.insertText("Hello")
cursor.setPosition(cursor.position() - 5)

在文档中正确插入文本 Hello 但将光标留在插入文本的末尾(而不是将其向左移动 5 个字符).第一行证明光标、文本编辑和文档设置正确.尝试 movePosition 也没有任何效果.

properly inserts the text Hello in the document but leaves the cursor at the end of the inserted text (instead of moving it to the left by 5 characters). The first line proves that cursor, textedit and document are set up properly. trying movePosition doesn't have any effect either.

实际目标是插入一些文本,选中它并将光标放在选择的末尾,如https://github.com/wbsoft/frescobaldi/blob/master/frescobaldi_app/cursortools.py#L179

The actual goal is to insert some text, have it selected and the cursor at the end of the selection as can be seen in https://github.com/wbsoft/frescobaldi/blob/master/frescobaldi_app/cursortools.py#L179

我在这里做错了什么吗?这可能是 Qt/PyQt 中的错误吗?或者这可能是 PyQt5 中的问题?

Am I doing anything wrong here? Could this be a bug in Qt/PyQt? Or could this be an issue in PyQt5?

我现在已经通过一个最小的应用程序示例确认问题不能出在应用程序的更大结构中.在下面的小应用程序中,setPositionmovePosition 都没有任何效果 - 而 insertText 效果很好:

I've now confirmed with a minimal app example that the problem can't be in the larger construction of the application. In the following mini app neither setPosition nor movePosition has any effect - while insertText works well:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

from PyQt5.QtWidgets import QApplication, QTextEdit

def main():    
    app = QApplication(sys.argv)

    w = QTextEdit()
    w.setWindowTitle('Manipulate cursor')
    cursor = w.textCursor()
    cursor.insertText("Hello World")
    # neither of the following commands have any effect
    cursor.setPosition(cursor.position() - 5)
    cursor.movePosition(cursor.movePosition(cursor.Left, cursor.KeepAnchor,  3))

    w.show()   
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

推荐答案

您正在处理 w.textCursor.您应该调用 w.setTextCursor(cursor) 在末尾更改可见光标.

You are working on a local copy of the text cursor returned by w.textCursor. You should call w.setTextCursor(cursor) at the end to change the visible cursor.

第二个问题是你使用movePosition的输出再次调用movePosition,这是不允许的:

A second problem is that you use the output of movePosition to call movePosition again, which is not allowed:

cursor.movePosition(cursor.movePosition(cursor.Left, cursor.KeepAnchor,  3))

应该

cursor.movePosition(cursor.Left, cursor.KeepAnchor,  3)

请注意,我在 Qt(不是 PyQt)中对其进行了测试,但这应该没有任何区别,它成功地选择了 Hello worldlo.

Note that I tested it in Qt (not PyQt), but that should not make any difference, which successfully selected lo of Hello world.

这篇关于在 PyQt5 文本编辑中移动光标不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆