from PyQt4 import QtGui, QtCore
import sys
def __init__(self):
super(Test, self).__init__(5,5)
# If you comment out these lines the contextMenuEvent slot will be
# called when the context menu is suppose to appear. If you leave them
# active the ctxMenu def is called when the context Menu is suppose
# to appear
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.ctxMenu)
def contextMenuEvent(self, event):
a
= QtGui.
QAction("Current Cell", self
) menu.addAction(a)
a.triggered.connect(self.contextCurPosEvent)
self.cursor_zero = event.pos()
menu.exec_(event.globalPos())
def ctxMenu(self, position):
a
= QtGui.
QAction("Current Cell", self
) menu.addAction(a)
a.triggered.connect(self.contextCurPos)
self.cursor_zero = position
# Because the QTable Widget is a scroll area the position passed to
# this def is actually with respect to the viewport of the widget
# therefore you need to call the mapToGlobal from the viewport and not
# the TableWidget
menu.exec_(self.viewport().mapToGlobal(position))
def contextCurPosEvent(self):
pos = self.cursor_zero
col = self.columnAt(pos.x())
row = self.rowAt(pos.y())
print('Map From Cursor %d, %d' % (row, col))
model_idx = self.model().createIndex(row, col)
model_idx = self.currentIndex()
print('From Current Index %d, %d' % (model_idx.row(), model_idx.column()))
print('\n')
def contextCurPos(self):
pos = self.cursor_zero
col = self.columnAt(pos.x())
row = self.rowAt(pos.y())
print('Map From Cursor %d, %d' % (row, col))
model_idx = self.model().createIndex(row, col)
model_idx = self.currentIndex()
print('From Current Index %d, %d' % (model_idx.row(), model_idx.column()))
print('\n')
x = Test()
x.show()
sys.exit(app.exec_())
from PyQt4 import QtGui, QtCore
import sys
class Test(QtGui.QTableWidget):
def __init__(self):
super(Test, self).__init__(5,5)
# If you comment out these lines the contextMenuEvent slot will be
# called when the context menu is suppose to appear. If you leave them
# active the ctxMenu def is called when the context Menu is suppose
# to appear
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.ctxMenu)
def contextMenuEvent(self, event):
menu = QtGui.QMenu(self)
a = QtGui.QAction("Current Cell", self)
menu.addAction(a)
a.triggered.connect(self.contextCurPosEvent)
self.cursor_zero = event.pos()
menu.exec_(event.globalPos())
def ctxMenu(self, position):
menu = QtGui.QMenu(self)
a = QtGui.QAction("Current Cell", self)
menu.addAction(a)
a.triggered.connect(self.contextCurPos)
self.cursor_zero = position
# Because the QTable Widget is a scroll area the position passed to
# this def is actually with respect to the viewport of the widget
# therefore you need to call the mapToGlobal from the viewport and not
# the TableWidget
menu.exec_(self.viewport().mapToGlobal(position))
def contextCurPosEvent(self):
pos = self.cursor_zero
col = self.columnAt(pos.x())
row = self.rowAt(pos.y())
print('Map From Cursor %d, %d' % (row, col))
model_idx = self.model().createIndex(row, col)
model_idx = self.currentIndex()
print('From Current Index %d, %d' % (model_idx.row(), model_idx.column()))
print('\n')
def contextCurPos(self):
pos = self.cursor_zero
col = self.columnAt(pos.x())
row = self.rowAt(pos.y())
print('Map From Cursor %d, %d' % (row, col))
model_idx = self.model().createIndex(row, col)
model_idx = self.currentIndex()
print('From Current Index %d, %d' % (model_idx.row(), model_idx.column()))
print('\n')
app = QtGui.QApplication(sys.argv)
x = Test()
x.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks