gpt4 book ai didi

python - PYQT如何编辑在MainWindow中tab化的QDockWidget的标题?

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:08 25 4
gpt4 key购买 nike

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()

关于python - PYQT如何编辑在MainWindow中tab化的QDockWidget的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40839899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com