简体   繁体   中英

PyQt QTableView After click, how to know row and col

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)

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM