Qt/PyQt dialog with toggable fullscreen mode - problem on Windows
- by Guard
I have a dialog created in PyQt. It's purpose and functionality don't matter.
The init is:
class MyDialog(QWidget, ui_module.Ui_Dialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.setupUi(self)
self.installEventFilter(self)
self.setWindowFlags(Qt.Dialog | Qt.WindowTitleHint)
self.showMaximized()
Then I have event filtering method:
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress:
key = event.key()
if key == Qt.Key_F11:
if self.isFullScreen():
self.setWindowFlags(self._flags)
if self._state == 'm':
self.showMaximized()
else:
self.showNormal()
self.setGeometry(self._geometry)
else:
self._state = 'm' if self.isMaximized() else 'n'
self._flags = self.windowFlags()
self._geometry = self.geometry()
self.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint)
self.showFullScreen()
return True
elif key == Qt.Key_Escape:
self.close()
return QWidget.eventFilter(self, obj, event)
As can be seen, Esc is used for dialog hiding, and F11 is used for toggling full-screen. In addition, if the user changed the dialog mode from the initial maximized to normal and possibly moved the dialog, it's state and position are restored after exiting the full-screen.
Finally, the dialog is created on the MainWindow action triggered:
d = MyDialog(self)
d.show()
It works fine on Linux (Ubuntu Lucid), but quite strange on Windows 7:
if I go to the full-screen from the maximized mode, I can't exit full-screen (on F11 dialog disappears and appears in full-screen mode again. If I change the dialog's mode to Normal (by double-clicking its title), then go to full-screen and then return back, the dialog is shown in the normal mode, in the correct position, but without the title line.
Most probably the reason for both cases is the same - the setWindowFlags doesn't work. But why?
Is it also possible that it is the bug in the recent PyQt version? On Ubuntu I have 4.6.x from apt, and on Windows - the latest installer from the riverbank site.