将多行添加到 QTextEdit PyQt [英] Add more than one line to a QTextEdit PyQt
本文介绍了将多行添加到 QTextEdit PyQt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 PyQT QTextEdit 遇到了一个相当奇怪的问题.
I am experiencing a rather strange issue with my PyQT QTextEdit.
当我从 QLineEdit 输入一个字符串时,它会添加它,但说我输入了另一个,第一个字符串消失了我认为这是因为我没有附加文本.
When I enter a string from my QLineEdit it adds it but say I enter another the first string disappears I assume that's because I am not appending the text.
知道我该怎么做吗?
相关代码如下:
self.mytext.setText(str(self.user) + ": " + str(self.line.text()) + "\n")
和重要的
self.mySignal.emit(self.decrypt_my_message(str(msg)).strip() + "\n")
编辑
我发现我需要使用 QTextCursor
self.cursor = QTextCursor(self.mytext.document())
self.cursor.insertText(str(self.user) + ": " + str(self.line.text()) + "\n")
推荐答案
setText()
方法替换当前所有的文本,所以你只需要使用 append()
代码> 方法代替.(请注意,这两种方法都会自动添加尾随换行符).
The setText()
method replaces all the current text, so you just need to use the append()
method instead. (Note that both these methods automatically add a trailing newline).
import sys
from PyQt4 import QtGui
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QVBoxLayout(self)
self.button = QtGui.QPushButton('Test')
self.edit = QtGui.QTextEdit()
layout.addWidget(self.edit)
layout.addWidget(self.button)
self.button.clicked.connect(self.handleTest)
def handleTest(self):
self.edit.append('spam: spam spam spam spam')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
这篇关于将多行添加到 QTextEdit PyQt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文