lskrinjar
lskrinjar

Reputation: 5793

PyQt - open only one child window and minimize it with parent window

The idea is to open a child window from parent window menu and when I minimize the parent window, the child window must be minimized also and only one child window can be opened. I have the solution for minimizing the child when parent is minimized, but I can open child window multiple-times (although the child is already opened) and I would like to disable opening of multiple child windows.

The parent window is MainWindow.py:

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle('Parent window')
        self.flags = QtCore.Qt.Window
        self.ControlPanel = Control_panel_window()

        self.createActions()
        self.createMenus()

    def createActions(self):
        #    window - menu
        self.windowShowControlPanelAction = QtGui.QAction(self.tr("&Control panel"), self, statusTip='Control panel')        
        self.connect(self.windowShowControlPanelAction, QtCore.SIGNAL("triggered()"), self.ShowControlPanel)

    def createMenus(self):
        #    window
        self.windowMenu = QtGui.QMenu(self.tr("&Window"), self)
        self.windowMenu.addAction(self.windowShowControlPanelAction)
        self.menuBar().addMenu(self.windowMenu)

    def ShowControlPanel(self):
        self.ControlPanel = Control_panel_window(self)
        self.ControlPanel.setWindowFlags(QtCore.Qt.Window)
        self.ControlPanel.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    win.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
    win.show() 
    sys.exit(app.exec_())

The child window is ChildWindow.py:

class Control_panel_window(QWidget):
    def __init__(self, parent = None):
        super(Control_panel_window, self).__init__(parent)
        self.setFixedSize(200, 300)

    def setWindowFlags(self, flags):
        print "flags value in setWindowFlags"
        print flags
        super(Control_panel_window, self).setWindowFlags(flags)

The problem is: how can I set that only one child window is opened?

Upvotes: 0

Views: 2883

Answers (1)

Bakuriu
Bakuriu

Reputation: 101939

In your ShowControlPanel function you are creating a new control panel each time the signal is triggered. Since you already have an instance available why don't you use that instead?

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle('Parent window')
        self.flags = QtCore.Qt.Window

        self.control_panel = ControlPanelWindow(self)
        self.control_panel.setWindowFlags(self.flags)

    #...

    def create_actions(self):
        self.show_control_panel_action = QtGui.QAction(
            self.tr("&Control panel"),
            self,
            statusTip='Control panel'
            )       
        self.show_control_panel_action.triggered.connect(self.show_control_panel)

    #...

    def show_control_panel(self):
        self.control_panel.show()

Some stylistic notes:

  • Try to follow PEP8 official python coding-style guide. This include using CamelCase for classes, lowercase_with_underscore for almost everything else. In this case, since Qt uses halfCamelCase for methods etc you may use it too for consistency.
  • Use the new-style signal syntax:

    the_object.signal_name.connect(function)
    

    instead of:

    self.connect(the_object, QtCore.SIGNAL('signal_name'), function)
    

    not only it reads nicer, but it also provides better debugging information. Using QtCore.SIGNAL you will not receive an error if the signal doesn't exist (e.g. you wrote a typo like trigered() instead of triggered()). The new-style syntax does raise an exception in that case you will be able to correct the mistake earlier, without having to guess why something is not working right and searching the whole codebase for the typo.

Upvotes: 1

Related Questions