In the attached example script, why won’t the MyDialog instance show properly when I set MyDialog’s parent to self on line 20 instead of leaving it blank? First I thought the shortcut had stopped working somehow, but obviously that’s not the case.
In this case it doesn’t really make any difference whether the parent is set, but in my real case I need the parent to be set.
Am I missing something obvious here?
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyDialog(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setFocusPolicy(Qt.StrongFocus)
label = QLabel(self)
label.setText("World")
hbox = QHBoxLayout()
hbox.addWidget(label)
self.setLayout(hbox)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.my_dialog = MyDialog()
#self.my_dialog = MyDialog(self)
label = QLabel(self)
label.setText("Hello")
self.setCentralWidget(label)
shortcut = QShortcut(QKeySequence(Qt.Key_Tab), self, self.show_my_dialog)
shortcut.setContext(Qt.ApplicationShortcut)
self.show()
def show_my_dialog(self):
md = self.my_dialog
if md.isVisible():
md.hide()
print 'hide'
else:
md.show()
print 'show'
def main():
app = QApplication([])
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
On my machine (Kubuntu 11.10) it’s working.
If Dialog’s parent is None – it opens another frameless window, and i have two entries in taskbar. If i close the main window, dialog remains.
If you are using Windows – the behavior you described might be related to it. Maybe the window is shown, it’s just behind other windows?
If Dialog’s parent is the main window – the frameless dialog is shown inside the main window – in the top left corner near the label.
You might be interested in this info: