Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt QTableView After click, how to know row and col

Tags:

python

pyqt

In table view, model, when you click on cell, what method do you know about cell row and column?

Version:
PyQt : 4.11.4
Python : 3.5.3

These are my setting of table view, model.

def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ntableView = QtGui.QTableView()
        self.nlayout = QtGui.QVBoxLayout()
        self.nmodel = QtGui.QStandardItemModel()
        self.ntableView.setModel(self.nmodel)
        self.nlayout.addWidget(self.ntableView)
        self.setLayout(self.nlayout)
        self.func_mappingSignal()

def func_mappingSignal(self):
        self.ntableView.clicked.connect(self.func_test)

def func_test(self, item):
        # http://www.python-forum.org/viewtopic.php?f=11&t=16817
        cellContent = item.data()
        print(cellContent)  # test
        sf = "You clicked on {}".format(cellContent)
        print(sf)
like image 881
redchicken Avatar asked Jul 18 '17 06:07

redchicken


1 Answers

If you want to get coordinates of clicked cell, you can use parameter of clicked signal handler, like you have called it item (it's QModelIndex in this case)

def func_test(self, item):

and get item.column(), item.row() values.

like

sf = "You clicked on {0}x{1}".format(item.column(), item.row())
like image 82
Lubomir Avatar answered Nov 15 '22 05:11

Lubomir