[PyQt] How do I subclass QTextEdit without warnings on close?

Brad Ralph lists at ralph.id.au
Tue May 3 05:31:29 BST 2011


Hi Zoli,

Thanks you're right.  I thought I must have missed the bleeding obvious <sigh> 
when it did not work before. It works fine now.

Cheers.


On Tue, 3 May 2011 01:29:10 PM Zoltan Szalai wrote:
> Hi Brad,
> 
> He means that you should call the focusOutEvent of the base class in
> your reimplementation of focusOutEvent.
> It's desired because QTextEdit reimplements focusOutEvent from QWidget.
> 
> Something like this:
>      def focusOutEvent(self, e):
>          QtGui.QTextEdit.focusOutEvent(self, e)
>          val = self.toHtml()
>          self.textEdited.emit(val)
> 
> or using super:
> 
>      def focusOutEvent(self, e):
>          super(MyQTextEdit, self).focusOutEvent(e)
>          val = self.toHtml()
>          self.textEdited.emit(val)
> 
> Either way, I don't get any error or warning at close.
> (Win7, 32bit, python: 2.6.5, pyqt: 4.8.3, qt: 4.7.1, sip: 4.12.1)
> 
> regards
> Zoli
> 
> On 2011.05.03. 4:49, Brad Ralph wrote:
> > Hi Pete,
> > 
> > Thanks for you reply but I don't understand what you are suggesting as a
> > solution. I tried to take you at your suggestion and replace the line
> > 'def  focusOutEvent(self, e):' with 'QtGui.QTextEdit.focusOutEvent(self,
> > e)' but it did not work. (Invalid syntax error). I even tried variations
> > like 'def QtGui.QTextEdit.focusOutEvent(self, e)' just in case I was
> > stupidly not understanding you.
> > 
> > I also experimented with my real application instead of the test app I
> > included in my email.  From my understanding of your diagnosis, if the
> > event does not get time to finish before the program ends, then I should
> > be able to enter into a different widget (which fires the event) then
> > wait for a few seconds (even 5 mins) for it to finish, before closing
> > the program.  When I do this the error still occurs, but only if I fire
> > the event by entering into the QTextEdit subclass.  This leads me to
> > think that I have no clue what is going on.  How can the event not
> > finish when several seconds have passed after it was fired?
> > 
> > I also wonder what is the correct way to subclass a Qt4 widget in this
> > case.I have been using the method in PyQt3 and it has always worked, so
> > I guess I need to do it differently now.
> > 
> > I also would like to know why you suggest using Super()?  I seem to
> > remember that there were warnings in the PyQt docs that using it in
> > certain cases could cause problems, so I've always avoided it.  I also
> > tend to think that it breaks the 'explicit is better than implicit rule'
> > but that is just an opinion.
> > 
> > Thanks in advance for your help.
> > 
> > Regards,
> > Brad
> > 
> > On Mon, 2 May 2011 11:36:44 PM Hans-Peter Jansen wrote:
> > 
> > [snip ...]
> > 
> >>> ########################################
> >>> 
> >>> #!/bin/env python
> >>> # -*- coding: utf-8 -*-
> >>> import sys
> >>> from PyQt4 import QtCore, QtGui
> >>> 
> >>> try:
> >>>      _fromUtf8 = QtCore.QString.fromUtf8
> >>> 
> >>> except AttributeError:
> >>>      _fromUtf8 = lambda s: s
> >>> 
> >>> class MyQTextEdit(QtGui.QTextEdit):
> >>>      textEdited = QtCore.pyqtSignal('QString')
> >>>      
> >>>      def __init__(self, *args):
> >>>          QtGui.QTextEdit.__init__(self, *args)
> >>>      
> >>>      def  focusOutEvent(self, e):
> >>>          val = self.toHtml()
> >>>          self.textEdited.emit(val)
> >> 
> >> The dtor of QTextEdit is called before the event finished.
> >> 
> >> Try this:
> >> 	  QtGui.QTextEdit.focusOutEvent(self, e)
> >> 
> >> You might want to switch to super() for calling into the base classes,
> >> though.
> >> 
> >>> class frmTest(QtGui.QMainWindow):
> >>>      def __init__(self, app, *args):
> >>>          QtGui.QMainWindow.__init__(self, *args)
> >>>          self.app = app
> >>>          self.mainWidget = QtGui.QFrame()
> >>>          self.setCentralWidget(self.mainWidget)
> >>>          self.fmeLayout = QtGui.QVBoxLayout(self.mainWidget)
> >>>          self.teDesc1 = MyQTextEdit(self.mainWidget)
> >>>          self.teDesc1.setObjectName(_fromUtf8("teDesc1"))
> >>>          self.fmeLayout.addWidget(self.teDesc1)
> >>>          self.teDesc2 = MyQTextEdit(self.mainWidget)
> >>>          self.teDesc2.setObjectName(_fromUtf8("teDesc1"))
> >>>          self.fmeLayout.addWidget(self.teDesc2)
> >>> 
> >>> if "__main__"==__name__:
> >>>      qapp = QtGui.QApplication(sys.argv)
> >>>      view = frmTest(qapp)
> >>>      view.show()
> >>>      qapp.exec_()
> >> 
> >> Cheers,
> >> Pete
> 
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

-- 
Regards,
Brad Ralph

TechnoGeeks Can Help Pty Ltd.
174 Heathcote Rd
Moorebank NSW 2170

p: 1300 558 034
f: 02 9731 2590
m: 0414 227 779
w: www.technogeeks.com.au


More information about the PyQt mailing list