[PyKDE] QApplication.notify gets QObject receiver args
alh at jenkon.com
alh at jenkon.com
Thu Apr 25 23:06:00 BST 2002
Howdy,
I've encountered some mysterious behavior under PyQt.
While watching receiver/event args in a qt.QApplication.notify
override, the first time a window is created/opened, the receiver arg
(for focus events on the new window) is an instance of qt.QLineEdit.
The second time the same type of window is created/opened, the receiver arg
is a qt.QObject instance (instead of the expected qt.QLineEdit).
Environment is Win2000, Python 2.1.3, Qt 3.0.3, PyQt 3.1.
For example, run the code below (which opens a window w/ a
"Click" button).
-- Click the "Click" button, opening a second window with two line-edit controls.
-- Tab twice, alternating focus between the two line-edits; notice that the receiver of the FocusIn
events are instances of qt.QLineEdit.
-- Click the "Click" button, closing the second window.
-- Click the "Click" button to create a new instance of the second window.
-- Tab twice again; notice that this time, the receiver of the FocusIn events are instances of
qt.QObject!
Here's the corresponding console transcript:
C:\>python test.py
focus-in <qt.QPushButton instance at 0080331C>
open <__main__.TestWin instance at 0080AF34>
focus-in <qt.QLineEdit instance at 007FD7AC>
focus-in <qt.QLineEdit instance at 007FD134>
focus-in <qt.QLineEdit instance at 007FD7AC>
focus-in <qt.QPushButton instance at 0080331C>
close <__main__.TestWin instance at 0080AF34>
open <__main__.TestWin instance at 0080AF34>
focus-in <qt.QObject instance at 007F5704>
focus-in <qt.QObject instance at 007F568C>
focus-in <qt.QObject instance at 007F5704>
focus-in <qt.QPushButton instance at 0080331C>
close <__main__.TestWin instance at 0080AF34>
In the real application that displays similar behavior, it is important to be able to
identify the receivers as the appropriate qt.QLineEdit instances--if references are
maintained to the qt.QLineEdit instances, it doesn't appear possible to associate the qt.QObject
instances with the qt.QLineEdit instances.
Isn't it odd that both instances of the second window have the same str()? (Tight
memory management?)
Any help is greatly appreciated,
Alan Harkreader
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import sys
import qt
class App(qt.QApplication):
def notify(self, receiver, event):
if event.type() == qt.QEvent.FocusIn:
print 'focus-in', receiver
return qt.QApplication.notify(self, receiver, event)
class MainWin(qt.QMainWindow):
def __init__(self):
qt.QMainWindow.__init__(self)
self.wnd = None
btn = qt.QPushButton('Click', self)
btn.resize(btn.sizeHint())
self.connect(btn, qt.SIGNAL('clicked()'), self.toggleWindow)
def toggleWindow(self):
if self.wnd:
print 'close', self.wnd
self.wnd.close(1)
self.wnd = None
else:
self.wnd = TestWin()
print 'open', self.wnd
self.wnd.show()
class TestWin(qt.QMainWindow):
def __init__(self):
qt.QMainWindow.__init__(self)
main = qt.QWidget(self)
self.setCentralWidget(main)
layout = qt.QVBoxLayout(main)
layout.addWidget(qt.QLineEdit(main))
layout.addWidget(qt.QLineEdit(main))
if __name__ == '__main__':
app = App(sys.argv)
win = MainWin()
win.show()
app.connect(app, qt.SIGNAL('lastWindowClosed()'), app, qt.SLOT('quit()'))
app.exec_loop()
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
More information about the PyQt
mailing list