2012-01-03 20 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_()) 
関連する問題