When you have an instance and you want to be able to manage its events
(since you can't subclass the original) you can either hotswap the event
method, or you can make use of the event filter.

Replacing the event method with a wrapped version is the less Qt approach.
Python lets you assign objects willy-nilly:

########
from functools import partial

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        ...
        def focusInEvent(self, e):
            self.clear()
            self.__class__.focusInEvent(self, e)

        self.plainTextEdit.focusInEvent = partial(focusInEvent,
self.plainTextEdit)
########

That is just a hacky way, but it works perfectly fine. The Qt-ish way is to
 use the event filter. You can designate another object to receive events
first and decide if it wants to handle them, and whether the event should
continue to be passed down to the original target. Just implement the
eventFilter() method on your MainWindow class, and receive the focus event
for your text edit:

########
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        ...
        self.plainTextEdit.installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj is self.plainTextEdit and event.type() == event.FocusIn:
            obj.clear()

        return True
########

In this case you would always want to return True because you are just
slotting in some extra behavior, but you always want the events to be
received by the original object.

-- justin




On Wed, Nov 7, 2012 at 1:52 AM, iMath <[email protected]> wrote:

> The UI is as following
>
> The UI is as following
> http://img.my.csdn.net/**uploads/201211/07/1352267120_**2257.jpg<http://img.my.csdn.net/uploads/201211/07/1352267120_2257.jpg>
>
> I saved  the  form as a file named re.ui
> Then I convert it into Python script named ui_re.py
> Third ,I create the source file with the name
> callFirstApp.pyw and import the ui_re.py to it
>
> The code in the callFirstApp.pyw is as shown here:
> @
>
> import sys
> from PySide.QtGui import *
> from ui_re  import *
>
> class MainWindow(QMainWindow, Ui_MainWindow):
>     def __init__(self, parent=None):
>         super(MainWindow, self).__init__(parent)
>         self.setupUi(self)
>         self.plainTextEdit.**setPlainText("Type code here...")
>
>
>
> if __name__ == '__main__':
>     app =QApplication(sys.argv)
>     frame = MainWindow()
>     frame.show()
>     app.exec_()
> @
> I want to reimplement the QPlainText’s focusInEvent in this file ,my code
> is as following
> (means let the default text "Type code here..." disappear when focus  in
> the QTextEdit’s editable area)
>
> @
> def focusInEvent(self, event):
>         self.clear()
>         QPlainTextEdit.focusInEvent(**self, event)
> @
> But I don’t know how to add the code to the file correctly ?
> Thanks in advance .
>
> --
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings:
> http://groups.google.com/group/python_inside_maya/subscribe
>

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to