I'm trying to implement "things" on a QTableView. So, I started to override its keyPressEvent method, as it's described everywhere:
if e.key() == QtCore.Qt.Key_F2 and self.tableau.hasFocus():
blabla
elif e.key() == QtCore.Qt.Key_Return and self.tableau.hasFocus():
blabla
#TEST
elif e.key() == QtCore.Qt.Key_Right:
print("right")
elif e.key() == QtCore.Qt.Key_Down:
print("down")
else:
print("waza")
I can handle the F2 and the Return events, they work. But not the Key_Down event ! The Key_right works as well, but not the Down. I think it's because QTableWidget "intercepts" some of the events. When normal letters are pressed, no event is "emitted" either, for example.
I would like to access all the events (or at least the Up and Down events), how can I do that ?
EDIT:
The view I use is basically a QTableView, which I modified a bit:
class ViewPerso(QtGui.QTableView):
def __init__(self, parent=None):
super(ViewPerso, self).__init__(parent)
self.parent = parent
def currentChanged(self, current, previous):
index = current.sibling(current.row(), 4)
try:
if not index.data() or len(self.parent.vidsSelected()) > 1:
return
except AttributeError:
pass
self.clicked.emit(current)
self.scrollTo(current)
def keyboardSearch(self, search):
pass
I do not use QTableWidget, it doesn't do what I want. I also subclassed the view delegate, but normally (normally), nothing should interfere.
Here is how I use my view: I load a model, a proxy, and then I use my view:
self.modele = ModelPerso()
self.modele.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
self.modele.setTable("videos")
self.modele.select()
self.proxy = QtGui.QSortFilterProxyModel()
self.proxy.setSourceModel(self.modele)
self.tableau = ViewPerso(self)
self.tableau.setModel(self.proxy)
self.tableau.setItemDelegate(ViewDelegate(self))
I don't know where the keypresses end up, for now, they just disappear. I think (I think), each widget has a way to treat keypresses event, and maybe QTableView ignores the keys up and down ?
Ok, I finally ended up doing this:
class ViewPerso (QtGui.QTableView):
def __init__(self, parent = None):
super(ViewPerso, self).__init__(parent)
self.parent = parent
def currentChanged (self, current, previous):
index = current.sibling(current.row(), 4)
try:
if not index.data() or len(self.parent.vidsSelected()) > 1:
return
except AttributeError:
pass
self.clicked.emit(current)
self.scrollTo(current)
def keyboardSearch (self, search):
pass
def keyPressEvent (self, e):
super(ViewPerso, self).keyPressEvent(e)
e.ignore()
The event was probably stuck in ViewPerso, so by overriding its keyPressEvent method like this, I get the usual treatments QTableWidget does on the events, and I the event is propagated to the parent widget, so I can use it again.
I tried to do what you showed in your edit, but it simply does not work for me, I don't really know why. My program is also very big (~10k lines), so the reason could come from anywhere. But thanks to make me realize where the error came from.