I literally hate to ask this question but I am having trouble with Qt 4 basically all I am trying to do is clear the textEdit area with clear() but I am not sure of what to do. In C++ I would just

textEdit->clear();

and it would do so. I am relatively new to python I have played around with it before.

Basically this is what I have got for the New Action

New = QtGui.QAction('New',self)
    New.setShortcut('Ctrl+N')
    New.setStatusTip('New File')
    
      
    self.connect(New,QtCore.SIGNAL('triggered()'),QtCore.SLOT('close()'))

I want to pass a function to it to clear textEdit.
I have tried defining a function and passing it but I can't seem to get it to work :(

Something like textedit.clear()?

no dice bro i put it in signal and slot i get no errors but it doesn't clear the textEdit form

I recently explained the Python equivalent of the pointer-to-member operator here, and you may want to read through that; however, I suspect that what you want is a reference to the function itself, rather than to call the member function.

In Python, the simple solution is to use... the name of the function. That's it. If you pass the function name without any parameters, it's the same (more or less) as a pointer to a function in C++.

The real problem I see is that you're passing a method rather than a regular function, which means that you would also have to pass an object of that class along with it, assuming you don't know ahead of time the class it is meant to operate on. Alternately, you could write a function that returns a closure containing the method invocation:

New = QtGui.QAction('New',self)
    New.setShortcut('Ctrl+N')
    New.setStatusTip('New File')
    
      
    self.connect(New,QtCore.SIGNAL('triggered()'),QtCore.SLOT('lambda x: x.close()'))

Whether this will work is uncertain to me, given the way Qt passes the functions to the slot. Perhaps an explicit function would work better, I'm not sure - I just don't know enough about Qt to say. Looking at the documentation, however, makes me think that something involving the pyqtSlot() decorator would be called for, and for all I know is already part of the textEdit object you are using.

Here is a simple example ...

# pqt_TextEdit2.py
# clear PyQT's QTextEdit multiline text entry box

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyFrame(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 50, 300, 180)
        self.setWindowTitle('QTextEdit() clear')

        self.edit = QTextEdit(self)
        self.edit.append('Just some text.')
        self.edit.append('You can append more.\n')
        self.edit.append('Click the right mouse button')
        self.edit.append('to get an option popup menu.')

        self.btn_clear = QPushButton(self)
        self.btn_clear.setText('Clear Text')

        # use grid layout manager
        grid = QGridLayout(self)
        # addWidget(QWidget, row, column, rowSpan, columnSpan)
        grid.addWidget(self.edit, 0, 0, 1, 1)
        grid.addWidget(self.btn_clear, 1, 0, 1, 2)
        self.setLayout(grid)

        # bind the button clicked to some action
        # newer connect style used with PyQT 4.5+
        self.btn_clear.clicked.connect(self.edit.clear)        


app = QApplication([])
frame = MyFrame()
frame.show()
app.exec_()

Note:
Python uses dot notation.
Also, you are not calling the function, just passing on the name of the function (reference).
If you have to pass arguments, use partial functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.