[PyKDE] Getting cell coordinates in QTable's contextMenuEvent (code
iacovou at gmail.com
Tue Dec 20 19:21:09 GMT 2005
I'm adding a context menu to a QTable widget, and am trying to work out which
cell has received the context menu event. I'm using the approach given in the
code snippet below.
I was originally stumped that the coordinates received by contextMenuEvent()
didn't seem to reflect the table being scrolled, but I found a workaround
based upon .
Now my problem is that the row/col system seems to be asymmetrical; that is:
(all clicks are 'right' clicks):
- clicking on the vertical header reports 'row = 0'
- clicking on the row labelled '1' reports 'row = 1'
- clicking on the horizontal header reports 'col = 0'
- clicking on the column labelled '1' reports 'col = 0'
- clicking in the last row (labelled '40') reports 'row = -1'
Does anyone have any idea why this is the case and what the 'right' way to
work around it would be?
from qt import *
from qttable import QTable
class MyTable ( QTable ):
def __init__ ( self, *args ):
QTable.__init__ ( self, *args )
def contextMenuEvent ( self, cmev ):
x = cmev.x()
y = cmev.y()
xoffset = + self.horizontalHeader().offset()
yoffset = + self.verticalHeader().offset()
col = self.columnAt ( x + xoffset )
row = self.rowAt ( y + yoffset )
print "X: ", x
print "Y: ", y
print "Xoff: ", xoffset
print "Yoff: ", yoffset
print "col: ", col
print "row: ", row
if __name__ == '__main__':
numRows = 40
numCols = 5
app = QApplication ( sys.argv )
table = MyTable ( numRows, numCols )
app.setMainWidget ( table )
More information about the PyQt