QDockWidget实例
#!/usr/bin/python # _*_ Coding: Utf-8 _*_ ''' QDockWidget funcs: setAllowedAreas(Qt::DockWidgetAreas areas) --- set allowed areas for dock widget setFeatures(DockWidgetFeatures features) --- set movable, floatable and closable setTitleBarWidget(QWidget *widget), setWidget(QWidget *widget) --- set title widget and content widget currentIndex() and currentWidget() --- return index and widget of current slots: setCurrentIndex(int index) setCurrentWidget(QWidget *widget) signals: featuresChanged(QDockWidget::DockWidgetFeatures features) --- features dockLocationChanged(Qt::DockWidgetArea area) --- location allowedAreasChanged(Qt::DockWidgetAreas allowedAreas) --- allowed area topLevelChanged(bool topLevel) --- toplevel visibilityChanged(bool visible) --- visibility --- edit by yangrui, 2020.2.13, 6.32 PM. ''' import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import (QApplication, QWidget, QDockWidget, QLabel, QComboBox) from PyQt5.QtGui import * class MystackedWidget(QWidget): qss = """ #dockWidget { border: 1px solid #FA4F9B; } #label1 { background: #545D4F; } #label2 { background: #FAA45D; } """ def __init__(self): super(MystackedWidget, self).__init__() self.initUI() def initUI(self): self.resize(300, 200) self.dockWidget = QDockWidget(self) self.dockWidget.setObjectName("dockWidget") label1 = QLabel("title bar widget") label1.setFixedSize(150, 20) label1.setObjectName("label1") label2 = QLabel("widget") label2.setFixedSize(150, 100) label2.setObjectName("label2") #self.dockWidget.setFloating(False) # floatable self.dockWidget.setAllowedAreas(Qt.TopDockWidgetArea | Qt.BottomDockWidgetArea) # Left, Right, Top, Bottom, No, All # set conbination of DockWidgetClosable, DockWidgetMovable and DockWidgetFloatable self.dockWidget.setFeatures(QDockWidget.DockWidgetClosable | QDockWidget.DockWidgetMovable | QDockWidget.DockWidgetFloatable) # set title bar widget and content widget #self.dockWidget.setTitleBarWidget(label1) self.dockWidget.setWidget(label2) ''' signals ''' self.dockWidget.topLevelChanged.connect(self.topLevel_handler) self.dockWidget.featuresChanged.connect(self.features_handler) self.dockWidget.allowedAreasChanged.connect(self.allowArea_handler) self.dockWidget.dockLocationChanged.connect(self.location_handler) self.dockWidget.visibilityChanged.connect(self.visibility_handler) self.setStyleSheet(self.qss) def topLevel_handler(self, isTopLevel): if isTopLevel: print("topLevel changed to " + str("true")) else: print("topLevel changed to " + str("false")) def features_handler(self, features): print("new features:", features) def allowArea_handler(self, allowArea): print("new allowed area: ", allowArea) def location_handler(self, area): print("new location: ", area) def visibility_handler(self, isVisible): if isVisible: print("dock widget is visible") else: print("dock widget isn't visible") if __name__ == '__main__': app = QApplication(sys.argv) form = MystackedWidget() form.show() app.exec_()