如何使用QDockWidget指定分隔符?

时间:2018-01-18 09:35:56

标签: qt pyqt qt5 pyqt5 qdockwidget

我有以下示例代码:

from PyQt5 import QtWidgets, QtCore, QtGui
import sys

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent=parent)
        self.bgcolor = self.palette().color(self.backgroundRole()).name()

        self.central = QtWidgets.QTextEdit(self)
        self.central.setText('this is the central widget')
        self.setCentralWidget(self.central)
        self.setDockOptions(self.AnimatedDocks) #prevent tabbing

        self.rightDock = QtWidgets.QDockWidget('right dock', self)
        self.rightDock.setAllowedAreas(QtCore.Qt.RightDockWidgetArea)
        self.rightDock.setStyleSheet('QDockWidget::title{text-align:left;background:'+self.bgcolor+';}')

        self.everywhereDock = QtWidgets.QDockWidget('everywhere dock',self)
        self.everywhereDock.setAllowedAreas(QtCore.Qt.BottomDockWidgetArea | QtCore.Qt.TopDockWidgetArea | QtCore.Qt.LeftDockWidgetArea | QtCore.Qt.RightDockWidgetArea)
        self.everywhereDock.setFeatures(QtWidgets.QDockWidget.DockWidgetFloatable | QtWidgets.QDockWidget.DockWidgetMovable)
        self.everywhereDock.setStyleSheet('QDockWidget::title{text-align:left;background:'+self.bgcolor+';}')

        self.dockable = QtWidgets.QTextEdit(self.rightDock)
        self.dockable.setText('this is dockable only on the right')
        self.dockable2 = QtWidgets.QTextEdit(self.everywhereDock)
        self.dockable2.setText('this is dockable everywhere, also its not closable')

        self.rightDock.setWidget(self.dockable)
        self.everywhereDock.setWidget(self.dockable2)

        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.rightDock)
        self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, self.everywhereDock)
        self.setTabPosition(QtCore.Qt.AllDockWidgetAreas, QtWidgets.QTabWidget.North)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    m = MainWindow()
    m.show()
    sys.exit(app.exec_())

问题是如何插入图标,其中DockWidgetAreas之间的边界是可拖动的,以便用户有一个提示,即有这个功能。

澄清: 我想在黑色箭头之间画一个图标:

enter image description here

1 个答案:

答案 0 :(得分:1)

QDockWidget支持“标题小部件”,它不是分隔符,但您可以使用QDockWidget::setTitleBarWidget(QWidget *widget)将其添加到每个QDockWidget中。

因此,您可以使用QWidget或其他内容创建一个通用QHorizontalLayout来保存此图标,并将其放入标题栏。 Qt处理的默认鼠标事件(例如拖动事件)应该继续正常工作,并且您有一个可自定义的标题栏。

self.rightDock = QtWidgets.QDockWidget('right dock', self)
self.rightDock.setAllowedAreas(QtCore.Qt.RightDockWidgetArea)

// add custom title widget
self.rightDock.setTitleBarWidget(self.titleWidget)
// add widget to dock widget
self.rightDock.setWidget(self.dockable)

self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.rightDock)