从 PyQt 中的 QTextEdit 获取可见文本

发布于 2024-11-17 13:32:14 字数 885 浏览 0 评论 0原文

这与我在这里发现的另一个问题有关,该问题似乎已经几个月不活跃了,所以我认为值得再次询问。

我创建了一个简单的 QDialog,它有一个 QTextEdit 和一个 QPushButton。当用户右键单击并选择“添加评论”选项时,它会在我的应用程序中弹出。我希望他们能够编写自由格式的文本,我只需将他们编写的任何内容保存为长字符串,而不关心新行等。

当用户单击按钮时,它会执行如下代码:

    self.connect(accept_button,QtCore.SIGNAL('clicked()'),lambda arg=str(view_textedit.toPlainText()): self.updateGroupComments(arg))

def updateGroupComments(self,new_comment_str):
    print "Updating user comment to have new string: " + new_comment_str
    self.group_entry.list_of_user_comments[self.currentFrameCounter] = new_comment_str

这不是检测可见的 TextEdit 文本(它仅检测创建文本编辑文本时设置的内容)。如何创建一个简单的命令,从 QTextEdit 返回当前可见的文本。同样,该功能

toPlainText()

无法正常工作...它找不到当前可见的文本,只能找到用户开始进行更改或添加之前屏幕上的任何文本。

如果在没有子类化和吸引光标位置的情况下无法完成此操作,那么整个事情看起来毫无价值......因此,请仅对那些无需子类化或操作光标而实现的建议保留建议。返回所有当前可见的文本应该非常简单明了......我错过了什么?

This is related to another question I found here that seems to be inactive for a few months, so I think it's worth asking again.

I have created a simple QDialog that has a QTextEdit and a QPushButton. This pops up in my application when a user right-clicks and selects the option to "add comments". I want them to be able to write free-form text and I'll just save whatever they write as a long string with no concern for new lines, etc.

When the user clicks the button, it executes code like this:

    self.connect(accept_button,QtCore.SIGNAL('clicked()'),lambda arg=str(view_textedit.toPlainText()): self.updateGroupComments(arg))

def updateGroupComments(self,new_comment_str):
    print "Updating user comment to have new string: " + new_comment_str
    self.group_entry.list_of_user_comments[self.currentFrameCounter] = new_comment_str

This is not detecting the TextEdit text that is visible (it only detects whatever the text edit text is set to when it is created). How do I make a simple command that returns the currently visible text from a QTextEdit. Again, the function

toPlainText()

is not working correctly... it doesn't find the currently visible text, only whatever text was on screen before changes or additions started being made by the user.

If this can't be done without subclassing and appealing to cursor positions, it makes the whole thing seem worthless... so please keep suggestions only to those implemented without subclassing or manipulating cursors. It should be really simple and straightforward to just return all currently visible text... what am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

邮友 2024-11-24 13:32:14

绑定到默认参数的对象在定义时进行评估。函数工作正常,它返回执行时文本字段中的任何内容。您的代码只是在错误的时刻调用了它。如果您想使用 lambda,则执行以下操作:

self.connect(
    accept_button, QtCore.SIGNAL('clicked()'),
    lambda: self.updateGroupComments(str(view_textedit.toPlainText()))
)

或者将 view_textedit 改为实例属性,然后简单地

self.connect(
    accept_button, QtCore.SIGNAL('clicked()'), self.updateGroupComments
)

updateGroupComments 更改为调用 self.view_textedit.toPlainText 而不是接受参数。

顺便说一句,这不是 PyQt 特有的,这是 Python 的一般工作方式。

为了说明我的最后评论,该 lambda 很好地可以替换为:

def slot():
    self.updateGroupComments(str(view_textedit.toPlainText()))

self.connect(accept_button, QtCore.SIGNAL('clicked()'), slot)

Objects that are being bound to default arguments are evaluated at the definition time. The function is working correctly, it returns whatever was in the text field when it was executed. Your code simply calls it at the wrong moment. If you want to use lambda, then do:

self.connect(
    accept_button, QtCore.SIGNAL('clicked()'),
    lambda: self.updateGroupComments(str(view_textedit.toPlainText()))
)

Or make view_textedit an instance attribute instead, and do simply

self.connect(
    accept_button, QtCore.SIGNAL('clicked()'), self.updateGroupComments
)

And change updateGroupComments to call self.view_textedit.toPlainText instead of taking an argument.

BTW, this is not PyQt specific, this is how Python works in general.

To illustrate my last comment, that lambda can very well be replaced with:

def slot():
    self.updateGroupComments(str(view_textedit.toPlainText()))

self.connect(accept_button, QtCore.SIGNAL('clicked()'), slot)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文