QT window closes immediately

Diez B. Roggisch deets at web.de
Tue Nov 16 19:54:16 EST 2010


Martin Caum <herrxandor at gmail.com> writes:

> I am attempting to open a window on mouse activity which works, but
> the window fails to stay open.
> I set it to terminate when the escape key is pressed even when the
> program is not currently selected. This works fine. Originally I had
> it create the window only with a right click, but when I noticed the
> window closed immediately I set it to any mouse activity for easier
> debugging. My script is as follows:
> [code]
> import pythoncom, pyHook
> import sys
> import win32api
> from PyQt4 import QtGui, QtCore
>
> class WindowObject(QtGui.QWidget):
> 	def __init__(self, parent=None):
> 		QtGui.QWidget.__init__(self, parent)
> 		self.setWindowTitle('Window')
> 		self.resize(50, 250)
>
> def OnKeyboardEvent(event):
> 	if event.KeyID == 27:
> 		win32api.PostQuitMessage()
> 	return 1
>
> def OnMouseEvent(event):
> 	x = event.Position[0]
> 	y = event.Position[1]
> 	createWindow(x, y)
> 	return 1
>
> def createWindow(x, y):
> 	menu = WindowObject()
> 	menu.move(x, y)
> 	menu.show()
>
> hm = pyHook.HookManager()
> hm.MouseAll = OnMouseEvent
> hm.KeyDown = OnKeyboardEvent
> hm.HookMouse()
> hm.HookKeyboard()
>
> app = QtGui.QApplication(sys.argv)
> pythoncom.PumpMessages()[/code]
> I'm fairly certain that this is due to my lack of understanding in the
> PumpMessages command and the HookManager. Any advice?

1) Keep a reference to your window-object. I can only guess (PyQt not
being a regular topic when hacking for me.. .sadly), but GC can
interfere with these kind of things.

2) what is the win32 stuff about? Qt should abstract from that.

3) if in doubt, as on the PyQt mailing list.

Diez



More information about the Python-list mailing list