当窗口最小化并恢复时,Pyqt底座会被隐藏

时间:2014-01-01 21:40:56

标签: windows pyqt4 restore minimize dock

当我最小化Windows XP上的应用程序窗口并稍后恢复时,将隐藏Dock。这与视图菜单有关,视图菜单可以切换以设置可见性,当然也可以通过信号连接。

我希望这可以节省几个小时的调试时间。

这是一个包含错误和正确代码的完整功能示例:

# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui

class Ui_QMainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(200, 200)
        self.menubar = QtGui.QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 27))
        self.menuMenu = QtGui.QMenu(self.menubar)
        self.setMenuBar(self.menubar)
        self.dock = QtGui.QDockWidget(self)
        self.dock.setObjectName("dock")
        self.dockContents = QtGui.QWidget()
        self.dockContents.setObjectName("dockContents")
        self.dock.setWidget(self.dockContents)
        self.addDockWidget(QtCore.Qt.DockWidgetArea(4), self.dock)
        self.action = QtGui.QAction(self)
        self.action.setCheckable(True)
        self.action.setChecked(True)
        self.action.setObjectName("action")
        self.menuMenu.addAction(self.action)
        self.menubar.addAction(self.menuMenu.menuAction())

        self.setWindowTitle("Example of dock remaining minimized")
        self.menuMenu.setTitle("Menu")
        self.dock.setWindowTitle("I'm a dock")
        self.action.setText("Dock visibility")

        if True:
            # This is NOT working on Windows XP.
            # Minimize the window and restore again, the dock is gone.
            # Other than that it works.
            QtCore.QObject.connect(self.action,
                                   QtCore.SIGNAL("toggled(bool)"),
                                   self.dock.setVisible)
            QtCore.QObject.connect(self.dock,
                                   QtCore.SIGNAL("visibilityChanged(bool)"),
                                   self.action.setChecked)
        else:
            # This DOES work, but boy it looks nasty, writing useless
            # per dock is not nice.
            QtCore.QObject.connect(self.action,
                                   QtCore.SIGNAL("triggered()"),
                                   self.toggle_dock)
            QtCore.QObject.connect(self.dock,
                                   QtCore.SIGNAL("visibilityChanged(bool)"),
                                   self.action.setChecked)

    def toggle_dock(self):
        self.dock.setVisible(not self.dock.isVisible())

def main():
    app = QtGui.QApplication(sys.argv)
    ui = Ui_QMainWindow()
    ui.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:3)

使用QDock.toggleViewAction有一种更简单的方法。此函数返回一个现成的操作,自动处理已检查的状态。

所以你的代码会变得简单:

    self.action = self.dock.toggleViewAction()
    self.action.setObjectName("action")
    self.menuMenu.addAction(self.action)
    self.menubar.addAction(self.menuMenu.menuAction())

    self.setWindowTitle("Example of dock remaining minimized")
    self.menuMenu.setTitle("Menu")
    self.dock.setWindowTitle("I'm a dock")
    self.action.setText("Dock visibility")

然后你可以摆脱所有的信号处理。