[PySide] right clicking on QTableView to get selected rows

Frank Rueter | OHUfx frank at ohufx.com
Tue Aug 28 11:41:47 CEST 2012


cool, thanks Tibold. That sounds like the right lead.
I will investigate...

On 28/08/12 9:37 PM, Tibold Kandrai wrote:
> Hi,
>
> There are multiple ways to deal with the context menus. You don't 
> necessarily need to use the mousePressEvent.
> I would use or the customContextMenuRequested signal or adding actions 
> to the view. Note you also need to set the contextMenuPolicy to 
> ActionsContextMenu or CustomContextMenu depending on which you use.
>
> References:
>
>   * http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#customContextMenuRequested
>   * http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#contextMenuPolicy-prop
>   * http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#addAction
>
> I am not sure though how the selection works exactly with the context 
> menu. But in the customContextMenuRequested you can get the right 
> clicked row's index easily like this:
> model_index = self.indexAt(point)
>
> Hope this helps,
> Tibold
>
>
> 2012/8/28 Frank Rueter | OHUfx <frank at ohufx.com <mailto:frank at ohufx.com>>
>
>     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
>     _______________________________________________
>     PySide mailing list
>     PySide at qt-project.org <mailto:PySide at qt-project.org>
>     http://lists.qt-project.org/mailman/listinfo/pyside
>
>
>
>
> -- 
> Kandrai Tibold

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20120828/7d075ca9/attachment.html>


More information about the PySide mailing list