2012-01-03 6 views
2

내부 부품의 탭 레이블 이름을 바꿀 수 있습니다.PyQt : 편집 가능한 탭 레이블

QInputDialog으로 새 라벨을 받고 탭 위젯 라벨을 설정할 수 있습니다.

하지만 라벨을 두 번 클릭하는 것과 같은 사용자 친화적 인 솔루션을 원하며 자체적으로 양식을 수정 해 주시기 바랍니다.

편집 가능한 플래그가있는 listWidgetItem은 나를 보여줄 수는 있지만 탭 라벨에 대한 해결책을 찾을 수 없습니다.

아무것도 나를 도울 수 있습니다 :

답변

6

이를 달성하기위한 기본 방법이 없습니다.

사용자는 자신의 탭 표시 줄을 구현하고 레이블 편집기를 직접 페인트해야합니다. 이는 분명 쉽지 않습니다.

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_()) 
+1

WOW !!!! 너는 내 구세주 야 !! 당신의 친절에 감사드립니다. 그것은 작동합니다! 나는 당신의 스크립트를 붙여 넣었고 조금 편집했다.) 그냥 "setTabBar" –

3

업데이트 : 그것은 여기에 편집

기본 탭 편집을 보여줍니다 스크립트의 등을 단지 아무 버튼 간단한 팝업 라인 편집,없이 제목 표시 줄, 수 ekhumoro 왼쪽 대답에 : PyQt5에서

코드는 이제 다음과 같습니다

from PyQt5.QtCore import Qt, QEvent 
from PyQt5.QtWidgets import QTabBar, QTabWidget, QApplication, QLineEdit, QWidget 

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

    def eventFilter(self, widget, event): 
     if ((event.type() == QEvent.MouseButtonPress and not self._editor.geometry().contains(event.globalPos())) or (event.type() == QEvent.KeyPress and event.key() == Qt.Key_Escape)): 
      self._editor.hide() 
      return True 
     return 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(QTabWidget): 
    def __init__(self): 
     QTabWidget.__init__(self) 
     self.setTabBar(EditableTabBar(self)) 
     self.addTab(QWidget(self), 'Tab One') 
     self.addTab(QWidget(self), 'Tab Two') 

if __name__ == '__main__': 

    import sys 
    app = QApplication(sys.argv) 
    window = Window() 
    window.show() 
    sys.exit(app.exec_())