PyQt - 只打开一个子窗口,并使用父窗口将其最小化

时间:2014-02-04 07:49:33

标签: python python-2.7 pyqt pyqt4

我们的想法是从父窗口菜单打开子窗口,当我最小化父窗口时,子窗口也必须最小化,并且只能打开一个子窗口。 我有最小化父项时最小化孩子的解决方案,但我可以多次打开子窗口(虽然孩子已经打开),我想禁用打开多个子窗口。

父窗口是 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_())

子窗口是 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)

问题是:如何设置只打开一个子窗口?

1 个答案:

答案 0 :(得分:1)

ShowControlPanel功能中,每次触发信号时都会创建一个新的控制面板。由于您已经有可用的实例,为什么不使用它呢?

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()

一些风格的笔记:

  • 尝试按照PEP8 官方 python编码风格指南进行操作。这包括使用CamelCase用于类,lowercase_with_underscore用于几乎所有其他类。 在这种情况下,由于Qt使用halfCamelCase方法等,您可能也会使用它来保持一致性。
  • 使用new-style signal syntax

    the_object.signal_name.connect(function)
    

    而不是:

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

    它不仅读得更好,而且还提供更好的调试信息。使用QtCore.SIGNAL如果信号不存在,您将不会收到错误(例如,您写了一个类似trigered()而非triggered()的拼写错误。新式语法确实引发异常,在这种情况下,您将能够更早地纠正错误,而不必猜测为什么某些东西不正常并在整个代码库中搜索错误。

相关问题