[PyKDE] contextMenuEvent for QTableView column headers

Mark Summerfield mark at qtrac.eu
Mon Jan 15 05:26:47 GMT 2007


On Sun 14-Jan-07 21:20, N. Volbers wrote:
> Hello everyone,
>
> I am relatively new to pyQt and I have a question regarding the
> QTableView.  I have derived a view class from QTableView class and I
> have implemented a context menu by adding a method like this:
>
>      def contextMenuEvent(self, event):
> 	self.datasetMenu.exec_(event.globalPos())
>
> where datasetMenu is a Menu I have created in the constructor.
>
> This works very nicely, but I noticed that the popup will only be
> created when clicking inside of the tableview. However, I would like to
> present a context menu when the user clicks on the column header.
>
> What can I do to achieve this?
>
> Niklas Volbers.

In Qt 4 the easiest way to get a context menu is _not_ to reimplement
the context menu event, but instead to 
(1) Set the context menu policy on the widget you are interested in,
i.e.  QWidget.setContextMenuPolicy(Qt.ActionsContextMenu), then
(2) Add the actions you want to appear in the menu to the widget with
QWidget.addAction(action1), etc.

So for your particular example, do this both to your QTableView, and to
the widget you get from QTableView.horizontalHeader().

I haven't tried this with a horizontalHeader() myself though.

-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu




More information about the PyQt mailing list