Issue
Using PySide 1.2
Grabbing the index of a clicked item in a QTableView
returns a different result wether there is a horizontal header or not.
If there is a horizontal header, it's seems to be included in the computing of visualRect
. So the first line, first column index is no longer 0, 0 but 1, 0.
Is this intended ? Because it is really confusing and inconvenient because the last line index will be invalid.
To reproduce :
import re
import operator
import os
import sys
# from Qt.QtCore import *
# from Qt.QtGui import *
# from Qt.QtWidgets import *
from PySide.QtCore import *
from PySide.QtGui import *
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class CustomTableView(QTableView):
"""
"""
def __init__(self, parent=None):
"""
"""
super(CustomTableView, self).__init__(parent=parent)
def mousePressEvent(self, mouse_event):
"""
"""
view_pos = self.mapFromGlobal(mouse_event.globalPos())
index = self.indexAt(view_pos)
print('mouse press index not cutting header : ' + str((index.row(), index.column())))
view_pos.setY(view_pos.y() - self.horizontalHeader().sizeHint().height())
index = self.indexAt(view_pos)
print('mouse press index cutting header : ' + str((index.row(), index.column())))
super(CustomTableView, self).mousePressEvent(mouse_event)
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
# create table
self.get_table_data()
table = self.createTable()
# layout
layout = QVBoxLayout()
layout.addWidget(table)
self.setLayout(layout)
def get_table_data(self):
stdouterr = os.popen4("dir c:\\")[1].read()
lines = stdouterr.splitlines()
lines = lines[5:]
lines = lines[:-2]
self.tabledata = [re.split(r"\s+", line, 4)
for line in lines]
def createTable(self):
# create the view
tv = CustomTableView()
# set the table model
header = ['date', 'time', '', 'size', 'filename']
tm = MyTableModel(self.tabledata, header, self)
tv.setModel(tm)
return tv
class MyTableModel(QAbstractTableModel):
def __init__(self, datain, headerdata, parent=None, *args):
""" datain: a list of lists
headerdata: a list of strings
"""
QAbstractTableModel.__init__(self, parent, *args)
self.arraydata = datain
self.headerdata = headerdata
def rowCount(self, parent):
return len(self.arraydata)
def columnCount(self, parent):
return len(self.arraydata[0])
def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.arraydata[index.row()][index.column()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.headerdata[col]
return None
if __name__ == "__main__":
main()
Solution
You do not have to use the global position and then pass it to the local position since you will get the position with respect to the upper corner of the QTableView, but indexAt requires the position with respect to the content according to the docs:
PySide.QtGui.QAbstractItemView.indexAt(point)
Parameters: point – PySide.QtCore.QPoint
Return type: PySide.QtCore.QModelIndex
Returns the model index of the item at the
viewport coordinates point
.
You must use the position of the event: mouse_event.pos()
def mousePressEvent(self, mouse_event):
index = self.indexAt(mouse_event.pos())
print('mouse press index: ' + str((index.row(), index.column())))
super(CustomTableView, self).mousePressEvent(mouse_event)
If you want to use the global position then you must convert it to a local position with respect to the viewport()
:
def mousePressEvent(self, mouse_event):
view_pos = self.viewport().mapFromGlobal(mouse_event.globalPos())
index = self.indexAt(view_pos)
print('mouse press index : ' + str((index.row(), index.column())))
super(CustomTableView, self).mousePressEvent(mouse_event)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.