views:

424

answers:

4

Hi Everybody,

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.

A: 

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()
Bruno Oliveira
+1  A: 

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()
gruszczy
A: 

Thanks guys for the comments.

When I try ....

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

it gives

AttributeError: text

it seems that the method text() belongs to QLineEdit, not to QTextEdit.

ThreaderSlash
this should be a comment - it's not an answer
gnud
all right... thanks for calling my attention.
ThreaderSlash
+2  A: 

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().

gnud
Since `toPlainText()` returns a `QString`, you may need to convert it so Python can print the text:`print "show input: %s" % str(self.textEditInput.toPlainText())`
jcoon
Nah, PyQt implements 'magic' python methods like `__str__`, you should be fine.
gnud
That's it... worked nicely. Thanks GNUD and COONJ -- it did the trick!
ThreaderSlash