2016-05-07 4 views
1

tableView's itemをクリックすると、PersistentEditorが開きます。オープンパーシステントデリゲートアイテムを照会する方法<code>tableView's item</code>をクリックすると、最初の列(整数データのため)は<code>QSpinBox</code>、その他の2つは<code>QLineEdit</code>にデフォルト設定されます。

onClickクリックされた行に対してすでにいくつの永続エディタが開いているかを照会したいと思います。

enter image description here

from PyQt4 import QtCore, QtGui 
app = QtGui.QApplication([]) 

class Model(QtCore.QAbstractTableModel): 
    def __init__(self): 
     QtCore.QAbstractTableModel.__init__(self) 
     self.items = [[1, 'one', 'ONE'], [2, 'two', 'TWO'], [3, 'three', 'THREE']] 

    def flags(self, index): 
     return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable 

    def rowCount(self, parent=QtCore.QModelIndex()): 
     return 3 
    def columnCount(self, parent=QtCore.QModelIndex()): 
     return 3 

    def data(self, index, role): 
     if not index.isValid(): return 

     if role in [QtCore.Qt.DisplayRole, QtCore.Qt.EditRole]: 
      return self.items[index.row()][index.column()] 

def onClick(index): 
    tableView.openPersistentEditor(tableView.model().index(index.row(), index.column())) 
    print 'clicked index: %s'%index 

tableModel=Model() 
tableView=QtGui.QTableView() 
tableView.setModel(tableModel) 
tableView.clicked.connect(onClick) 

tableView.show() 
app.exec_() 

答えて

1

QTは、あなたが欲しいものを実行する方法を提供することができます。もしそうなら、あなたはドキュメントを見て、何も見つけられなかったと思います。

それは、このようなモデルの何かにeditorCount()メソッドを定義するために働くだろうかしら:

def editorCount(index): 
    try: 
     rval = self.editor_count[index] 
     self.editor_count[index] += 1 
    except AttributeError: 
     self.editor_count = {} 
     self.editor_count[index] = 1 
     rval = 0 
    except KeyError: 
     self.editor_count[index] = 1 
     rval = 0 
    return rval 

その後のonClickはそれを呼び出すいます

def onClick(index): 
    tableView.openPersistentEditor(tableView.model().index(index.row(), index.column())) 
    current_editors = tableView.model().editor_count() 
    print 'clicked index: %s'%index 

を理想的には、もちろん、あなたをeditor_countディクショナリをのinitに定義し、editorCount()メソッド自体で例外処理をあまり必要としません。

関連する問題

 関連する問題