简体   繁体   English

Qt Python:QTextEdit-显示输入

[英]Qt Python: QTextEdit - display input

I have a QTextEdit... it works with 'clear()' when a pushbutton calls 'CleanComments' to clean the input done by the user. 我有一个QTextEdit ...当按钮调用“ CleanComments”以清理用户所做的输入时,它可以与“ clear()”一起使用。 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' "". 唯一的问题是,当调用“ showInput”以使用“ show()”在QTextEdit上显示内容时,它将给出“”显示输入:“无”“”。 So, what is missing here? 那么,这里缺少什么呢?

All comments and suggestions are highly appreciated. 所有意见和建议均受到高度赞赏。

To get the contents of a QTextEdit as a simple string, use the toPlainText() method. 要将QTextEdit的内容作为简单字符串获取,请使用toPlainText()方法。

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

There is also the toHtml() method. 还有toHtml()方法。 For even more options, you can work directly with the QTextDocument from QTextEdit.document() . 要获得更多选项,可以直接从QTextEdit.document()使用QTextDocument

Your showInput method is printing the return from the show() method, which returns None. 您的showInput方法正在打印show()方法的返回值,该方法返回None。 If you want to print the current text in the edit, use: 如果要在编辑中打印当前文本,请使用:

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

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. 例如,如果您有主窗口,则调用show将其显示给用户。 If you wish to retrieve data from some edit, be it line edit or text edit, you should use text() method. 如果希望从某些编辑(行编辑或文本编辑)中检索数据,则应使用text()方法。 Like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM