1

Clicking the tableView's item opens a PersistentEditor: it defaults to QSpinBox for the first column (since integer data) and QLineEdit for two others.

onClick I would like to query how many persistent editors have been already opened for the clicked row.

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_()
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

1 Answers1

1

QT may provide a way to do what you want. If so, I assume you've looked through the docs and not found anything.

I wonder if it would work to define an editorCount() method on your model something like this:

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

Then have onClick call it:

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

Ideally, of course, you'd define the editor_count dictionary in init and wouldn't need so much exception handling in the editorCount() method itself.

Tom Barron
  • 1,554
  • 18
  • 23