PDA

View Full Version : default QTableView behavior question



nlgootee
1st July 2016, 19:38
I have a tableview with a column that has selection and editing disabled. When I click on a row in that column and press a key, it jumps to the next row that starts with the key that I pressed. I can't find anything in the documentation about this behavior. Is it possible to turn it off?

jefftee
1st July 2016, 20:51
Is it possible to turn it off?
First, I believe this behavior is pretty standard, or at least in my experience, I would expect a view to work exactly as you described. If, however, you do want to change that behavior, I believe you are going to have to override the virtual function named keyPressEvent for QTableView. I'm not sure if you already have a QTableView subclass or not, but much like your ManifestModel subclass of QSqlTableModel, I believe you'll have to do the same for QTableView.

By default, the event object you will receive is already marked as "accepted". If you simply return, they key press won't be passed to any other event filters, effectively eating it.

If you wish to take no action on *any* key presses for your QTableView, then you should create a subclass of a QTableView and implement a method named keyPressEvent similar to the code below:



def keyPressEvent(self, event):
return


You can alternatively inspect the event object itself to figure out which key was actually pressed and selectively determine whether or not you want to "accept" the keypress. If you want no action to be taken, simply return, or invoke QTableView.keyPressEvent(event) for the default behavior for that key press.

There may be other ways to accomplish this, for example I believe you could install an event filter on your QTableView instance without having to subclass it, but effectively you need to get access to the event and either accept ("eat" it) or invoke the base class's event to achieve the default behavior.

Good luck.