python – PYQT如何编辑在MainWindow中标记的QDockWidget的标题?

from PyQt4 import QtCore, QtGui

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.centralContent = QtGui.QMainWindow()

        self.setCentralWidget(self.centralContent)

        self.centralContent.firstTabWidget = QtGui.QWidget()
        self.centralContent.firstTabDock = QtGui.QDockWidget("first")
        self.centralContent.firstTabDock.setWidget(self.centralContent.firstTabWidget)
        self.centralContent.addDockWidget(QtCore.Qt.LeftDockWidgetArea,self.centralContent.firstTabDock)
        self.centralContent.secondTabWidget = QtGui.QWidget()
        self.centralContent.secondTabDock = QtGui.QDockWidget("second")
        self.centralContent.secondTabDock.setWidget(self.centralContent.secondTabWidget)
        self.centralContent.addDockWidget(QtCore.Qt.LeftDockWidgetArea,self.centralContent.secondTabDock)

        self.centralContent.tabifyDockWidget(self.centralContent.firstTabDock, self.centralContent.secondTabDock)


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    app.exec_()

这是我的示例代码.我想用鼠标双击编辑DockWidget的标题(‘first’,’second’).这个问题有样品或类吗?

最佳答案 这是@JosephIreland解决方案的PyQt4端口(有一些改进):

class DockTitleBar(QtGui.QWidget):
    def __init__(self, dockWidget):
        super(DockTitleBar, self).__init__(dockWidget)

        boxLayout = QtGui.QHBoxLayout(self)
        boxLayout.setSpacing(1)
        boxLayout.setMargin(1)

        self.titleLabel = QtGui.QLabel(self)
        self.titleEdit = QtGui.QLineEdit(self)
        self.titleEdit.hide()
        self.titleEdit.editingFinished.connect(self.finishEdit)

        iconSize = QtGui.QApplication.style().standardIcon(
            QtGui.QStyle.SP_TitleBarNormalButton).actualSize(
                QtCore.QSize(100, 100))
        buttonSize = iconSize + QtCore.QSize(4, 4)

        self.dockButton = QtGui.QToolButton(self)
        self.dockButton.setIcon(QtGui.QApplication.style().standardIcon(
            QtGui.QStyle.SP_TitleBarNormalButton))
        self.dockButton.setMaximumSize(buttonSize)
        self.dockButton.setAutoRaise(True)
        self.dockButton.clicked.connect(self.toggleFloating)

        self.closeButton = QtGui.QToolButton(self)
        self.closeButton.setMaximumSize(buttonSize)
        self.closeButton.setAutoRaise(True)
        self.closeButton.setIcon(QtGui.QApplication.style().standardIcon(
            QtGui.QStyle.SP_DockWidgetCloseButton))
        self.closeButton.clicked.connect(self.closeParent)

        boxLayout.addSpacing(2)
        boxLayout.addWidget(self.titleLabel)
        boxLayout.addWidget(self.titleEdit)
        boxLayout.addStretch()
        boxLayout.addSpacing(5)
        boxLayout.addWidget(self.dockButton)
        boxLayout.addWidget(self.closeButton)

        dockWidget.featuresChanged.connect(self.onFeaturesChanged)

        self.onFeaturesChanged(dockWidget.features())
        self.setTitle(dockWidget.windowTitle())

        dockWidget.installEventFilter(self)

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.WindowTitleChange:
            self.setTitle(source.windowTitle())
        return super(DockTitleBar, self).eventFilter(source, event)

    def startEdit(self):
        self.titleLabel.hide()
        self.titleEdit.show()
        self.titleEdit.setFocus()

    def finishEdit(self):
        self.titleEdit.hide()
        self.titleLabel.show()
        self.parent().setWindowTitle(self.titleEdit.text())

    def onFeaturesChanged(self, features):
        if not features & QtGui.QDockWidget.DockWidgetVerticalTitleBar:
            self.closeButton.setVisible(
                features & QtGui.QDockWidget.DockWidgetClosable)
            self.dockButton.setVisible(
                features & QtGui.QDockWidget.DockWidgetFloatable)
        else:
            raise ValueError('vertical title bar not supported')

    def setTitle(self, title):
        self.titleLabel.setText(title)
        self.titleEdit.setText(title)

    def toggleFloating(self):
        self.parent().setFloating(not self.parent().isFloating())

    def closeParent(self):
        self.parent().toggleViewAction().setChecked(False)
        self.parent().hide()

    def mouseDoubleClickEvent(self, event):
        if event.pos().x() <= self.titleLabel.width():
            self.startEdit()
        else:
            # this keeps the normal double-click behaviour
            super(DockTitleBar, self).mouseDoubleClickEvent(event)

    def mouseReleaseEvent(self, event):
        event.ignore()

    def mousePressEvent(self, event):
        event.ignore()

    def mouseMoveEvent(self, event):
        event.ignore()
点赞