Add more than one line to a QTextEdit PyQt

前端 未结 1 1404
失恋的感觉
失恋的感觉 2020-11-30 11:58

I am experiencing a rather strange issue with my PyQT QTextEdit.

When I enter a string from my QLineEdit it adds it but say I enter another the first string disappea

相关标签:
1条回答
  • 2020-11-30 12:25

    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_())
    
    0 讨论(0)
提交回复
热议问题