Qt Python: QTextEdit - display input

ⅰ亾dé卋堺 提交于 2019-12-12 05:57:34

问题


I have a QTextEdit... it works with 'clear()' when a pushbutton calls 'CleanComments' to clean the input done by the user. Here is the code:

def CleanComments(self):
    self.textEditInput.clear()

def showInput(self):
    print "show input: %s" % self.textEditInput.show()

def buildEditInput(self):
    self.textEditInput = QtGui.QTextEdit(self.boxForm)
    self.textEditInput.setGeometry(QtCore.QRect(10, 300, 500, 100)) 

The only problem is, that when 'showInput' is called to display the content on QTextEdit using "show()", it gives "" show input: 'None' "". So, what is missing here?

All comments and suggestions are highly appreciated.


回答1:


To get the contents of a QTextEdit as a simple string, use the toPlainText() method.

print "show input: %s" % self.textEditInput.toPlainText()

There is also the toHtml() method. For even more options, you can work directly with the QTextDocument from QTextEdit.document().




回答2:


Your showInput method is printing the return from the show() method, which returns None. If you want to print the current text in the edit, use:

print "show input: %s" % self.textEditInput.text()



回答3:


Method show from widget is used to display the widget on a screen. For example if you have main window, you call show to display it to user. If you wish to retrieve data from some edit, be it line edit or text edit, you should use text() method. Like this:

def showInput(self):
    print "show input: %s" % self.textEditInput.text()


来源:https://stackoverflow.com/questions/2063633/qt-python-qtextedit-display-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!