2016-05-07 12 views
1

При нажатии на tableView's item открывается PersistentEditor: по умолчанию он равен QSpinBox для первого столбца (с целыми данными) и QLineEdit для двух других.Как запросить открытый постоянный элемент делегата

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 

В идеале, конечно, вам 'd определите словарь editor_count в init и не потребуется столько обработки исключений в самом методе editorCount().

 Смежные вопросы

  • Нет связанных вопросов^_^