从 PyQt 中的 QTextEdit 获取可见文本
这与我在这里发现的另一个问题有关,该问题似乎已经几个月不活跃了,所以我认为值得再次询问。
我创建了一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定到默认参数的对象在定义时进行评估。函数工作正常,它返回执行时文本字段中的任何内容。您的代码只是在错误的时刻调用了它。如果您想使用 lambda,则执行以下操作:
或者将
view_textedit
改为实例属性,然后简单地将
updateGroupComments
更改为调用self.view_textedit.toPlainText 而不是接受参数。
顺便说一句,这不是 PyQt 特有的,这是 Python 的一般工作方式。
为了说明我的最后评论,该 lambda 很好地可以替换为:
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:
Or make
view_textedit
an instance attribute instead, and do simplyAnd change
updateGroupComments
to callself.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: