[PyKDE] contextMenuEvent for QTableView column headers

N. Volbers mithrandir42 at web.de
Mon Jan 15 05:56:51 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.

Thanks for your reply.

It works for the horizontalHeader() as well.

The final solution to my problem looks a little different, I am now 
calling a custom function, because I would like to check some things 
before displaying the context menu:

         self.connect(self, SIGNAL("customContextMenuRequested(const 
QPoint &)"), self.on_context_menu_table)

         hdr = self.horizontalHeader()
         hdr.connect(hdr, SIGNAL("customContextMenuRequested(const 
QPoint &)"), self.on_context_menu_header)

In the handler, I am using something like this


Best regards,

Niklas Volbers.

More information about the PyQt mailing list