python - PyQt:可编辑的 QTabWidget 选项卡文本

标签 python pyqt pyqt4 qtabwidget

我可以通过编程方式重命名选项卡标签。

使用 QInputDialog 我可以获得新的标签文本并设置标签小部件标签。

但是,我希望有一个更加用户友好的解决方案,例如双击标签并在标签本身上进行编辑。

带有可编辑标志的QListWidgetItem可以给我指路,但我找不到标签标签的解决方案。

最佳答案

没有实现此目的的内置方法。但是,您可以使用简单的弹出行编辑并将其放置在选项卡上。这是一个基本的演示脚本:

enter image description here

PyQt5:

import sys
from PyQt5 import QtCore, QtWidgets

class TabBar(QtWidgets.QTabBar):
    def __init__(self, parent):
        super().__init__(parent)
        self._editor = QtWidgets.QLineEdit(self)
        self._editor.setWindowFlags(QtCore.Qt.Popup)
        self._editor.setFocusProxy(self)
        self._editor.editingFinished.connect(self.handleEditingFinished)
        self._editor.installEventFilter(self)

    def eventFilter(self, widget, event):
        if ((event.type() == QtCore.QEvent.MouseButtonPress and
             not self._editor.geometry().contains(event.globalPos())) or
            (event.type() == QtCore.QEvent.KeyPress and
             event.key() == QtCore.Qt.Key_Escape)):
            self._editor.hide()
            return True
        return super().eventFilter(widget, event)

    def mouseDoubleClickEvent(self, event):
        index = self.tabAt(event.pos())
        if index >= 0:
            self.editTab(index)

    def editTab(self, index):
        rect = self.tabRect(index)
        self._editor.setFixedSize(rect.size())
        self._editor.move(self.parent().mapToGlobal(rect.topLeft()))
        self._editor.setText(self.tabText(index))
        if not self._editor.isVisible():
            self._editor.show()

    def handleEditingFinished(self):
        index = self.currentIndex()
        if index >= 0:
            self._editor.hide()
            self.setTabText(index, self._editor.text())

class Window(QtWidgets.QTabWidget):
    def __init__(self):
        super().__init__()
        self.setTabBar(TabBar(self))
        self.addTab(QtWidgets.QWidget(self), 'Tab One')
        self.addTab(QtWidgets.QWidget(self), 'Tab Two')

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setWindowTitle('Test')
    window.setGeometry(600, 100, 200, 100)
    window.show()
    sys.exit(app.exec_())

PyQt4:

from PyQt4 import QtGui, QtCore

class TabBar(QtGui.QTabBar):
    def __init__(self, parent):
        QtGui.QTabBar.__init__(self, parent)
        self._editor = QtGui.QLineEdit(self)
        self._editor.setWindowFlags(QtCore.Qt.Popup)
        self._editor.setFocusProxy(self)
        self._editor.editingFinished.connect(self.handleEditingFinished)
        self._editor.installEventFilter(self)

    def eventFilter(self, widget, event):
        if ((event.type() == QtCore.QEvent.MouseButtonPress and
             not self._editor.geometry().contains(event.globalPos())) or
            (event.type() == QtCore.QEvent.KeyPress and
             event.key() == QtCore.Qt.Key_Escape)):
            self._editor.hide()
            return True
        return QtGui.QTabBar.eventFilter(self, widget, event)

    def mouseDoubleClickEvent(self, event):
        index = self.tabAt(event.pos())
        if index >= 0:
            self.editTab(index)

    def editTab(self, index):
        rect = self.tabRect(index)
        self._editor.setFixedSize(rect.size())
        self._editor.move(self.parent().mapToGlobal(rect.topLeft()))
        self._editor.setText(self.tabText(index))
        if not self._editor.isVisible():
            self._editor.show()

    def handleEditingFinished(self):
        index = self.currentIndex()
        if index >= 0:
            self._editor.hide()
            self.setTabText(index, self._editor.text())

class Window(QtGui.QTabWidget):
    def __init__(self):
        QtGui.QTabWidget.__init__(self)
        self.setTabBar(TabBar(self))
        self.addTab(QtGui.QWidget(self), 'Tab One')
        self.addTab(QtGui.QWidget(self), 'Tab Two')

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setWindowTitle('Test')
    window.setGeometry(600, 100, 200, 100)
    window.show()
    sys.exit(app.exec_())

关于python - PyQt:可编辑的 QTabWidget 选项卡文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8707457/

相关文章:

python - python是如何实现拼接的?

python - python 2.6 vr 中的列表理解和 lambda 表达式。 python 2.7

python - 如何更改文本与其边缘之间的 QLineEdit 间距

python - 如何使 PyQt 中的菜单项变灰

python - 无法填充 QTableWidget

python - PyQt4 使用 setRowHidden 在 QListView 上按文本进行过滤

python - 使用 Flask 和原生 Python 日志记录?

python - 如何确定小数是否可以完全表示为 Python float?

python - 在 Python 中使用 PyQt 将数据从 sqlite 数据库插入到 QTableWidget

python - PyQt中带有QThread的后台线程