[PySide] right clicking on QTableView to get selected rows

Frank Rueter | OHUfx frank at ohufx.com
Tue Aug 28 09:20:38 CEST 2012


Hi everybody,

I'm trying to implement a simple right click menu for a QTableView to 
work with the selected rows. The basics seem to work nicely but it seems 
like the selectionModel() gets updated AFTER the mousePressEvent 
registered, meaning if the right selection that happens on right lcick 
is not yet included in the  selecetdRows() return value.
How do I best ensure that whatever row is right clicked is included in 
the selectionModel().selectedRows(0)?
Here is my code:


class ToolsView(QTableView):
     def __init__(self, parent=None):
         super(ToolsView, self).__init__(parent)
         self.setSortingEnabled(True)
         self.setEditTriggers(QAbstractItemView.NoEditTriggers)
         self.horizontalHeader().setStretchLastSection(True)
self.setSelectionBehavior(QAbstractItemView.SelectRows)

     def mousePressEvent(self, event):
         mouseBtn = event.button()
         if mouseBtn == Qt.MouseButton.RightButton:
             print self.selectionModel().selectedRows(0)
         super(InstalledToolsView, self).mousePressEvent(event)


Cheers,
frank



More information about the PySide mailing list