PyQt:可编辑标签标签(PyQt: Editable Tab Labels)

编程入门 行业动态 更新时间:2024-10-19 17:25:12
PyQt:可编辑标签标签(PyQt: Editable Tab Labels)

我可以在内部部分重命名标签标签。

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

但是,我希望更多用户友好的解决方案,如双击标签并自行编辑表单。

带有可编辑标志的listWidgetItem可以向我显示方式,但我找不到标签标签的解决方案。

任何事情都可以帮助我:)

I can rename tab label at internal part.

With QInputDialog I can get new label and set tab widget label.

But, I hope more user-friendly solution like double-clicking on label and get editing form on itself.

A listWidgetItem with editable flag can show me the way, but I can't find the solution for tab label.

Anything can help me :)

最满意答案

没有内置的方法来实现这一点。

你将不得不实现自己的标签栏并自己绘制标签编辑器,这显然不容易。

而不是QInputDialog ,您可以创建自己的简化对话框来编辑标签。 它可能只是一个简单的弹出行编辑,没有按钮,也没有标题栏等。

编辑

这是一个演示基本选项卡编辑的脚本:

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.show() sys.exit(app.exec_())

There are no built-in methods for achieving this.

You would have to implement your own tab-bar and paint the label-editor yourself, which would obviously not be easy.

Instead of QInputDialog, you could create your own simplified dialog for editing labels. It could be just a simple popup line-edit, with no buttons, and no title bar, etc.

EDIT

Here's a script which demonstrates basic tab editing:

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.show() sys.exit(app.exec_())

更多推荐

本文发布于:2023-07-16 12:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   编辑   PyQt   Labels   Tab

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!