收集所有停靠的小部件及其位置

时间:2017-11-09 16:15:10

标签: python qt pyside qmainwindow qdockwidget

PySide中是否有办法收集所有停靠的小部件,以及它们是否停靠在主窗口的左侧或右侧?

如果我的第一个问题没有答案,我怎么能至少知道小部件是停靠在左边还是右边?

from PySide import QtCore, QtGui


class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

        self.createActions()
        self.createMenus()
        self.createToolBars()
        self.createStatusBar()
        self.createDockWindows()

        self.setWindowTitle("Dock Widgets")

    def createActions(self):
        self.toggleLeftAct = QtGui.QAction(
                "&Toggle Left", self, shortcut=QtGui.QKeySequence("Ctrl+L"),
                statusTip="Toggle Left Sidebar",
                triggered=self.toggleLeft)

        self.toggleRightAct = QtGui.QAction(
                "&Toggle Right", self, shortcut=QtGui.QKeySequence("Ctrl+R"),
                statusTip="Toggle Right Sidebar",
                triggered=self.toggleRight)

    def createMenus(self):
        self.fileMenu = self.menuBar().addMenu("&File")
        self.fileMenu.addAction(self.toggleLeftAct)
        self.fileMenu.addAction(self.toggleRightAct)

        self.viewMenu = self.menuBar().addMenu("&View")

    def createToolBars(self):
        self.fileToolBar = self.addToolBar("File")
        self.fileToolBar.addAction(self.toggleLeftAct)
        self.fileToolBar.addAction(self.toggleRightAct)

    def createStatusBar(self):
        self.statusBar().showMessage("Ready")

    def createDockWindows(self):
        self.dockCustomers = QtGui.QDockWidget("Customers", self)
        self.dockCustomers.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea | QtCore.Qt.RightDockWidgetArea)
        self.customerList = QtGui.QListWidget(self.dockCustomers)
        self.dockCustomers.setWidget(self.customerList)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dockCustomers)
        self.viewMenu.addAction(self.dockCustomers.toggleViewAction())

        self.dockParagraphs = QtGui.QDockWidget("Paragraphs", self)
        self.paragraphsList = QtGui.QListWidget(self.dockParagraphs)
        self.dockParagraphs.setWidget(self.paragraphsList)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dockParagraphs)
        self.viewMenu.addAction(self.dockParagraphs.toggleViewAction())

    def toggleLeft(self):
        print self.dockCustomers
        print self.dockParagraphs

    def toggleRight(self):
        print 'toggle Right'

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

获取停靠窗口小部件列表没有便捷方法。但是,每当添加dock-widget时,它将自动重新定位到主窗口,因此您可以使用主窗口的findChildren方法:

for dock in self.findChildren(QtGui.QDockWidget):
    print(dock.windowTitle())

可以使用主窗口的dockWidgetArea方法找到dock-widget当前所在的区域:

    area = self.dockWidgetArea(dock)
    if area == QtCore.Qt.LeftDockWidgetArea:
        print dock.windowTitle(), '(Left)'
    elif area == QtCore.Qt.RightDockWidgetArea:
        print dock.windowTitle(), '(Right)'