<br><br><div class="gmail_quote">On Fri, Mar 6, 2009 at 12:05 PM, Jim Bublitz <span dir="ltr"><<a href="mailto:jbublitz@nwinternet.com">jbublitz@nwinternet.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div><span id="q_11fdd14ff0dc544f_0" class="h4">- Show quoted text -</span></div><div class="h5">On Friday 06 March 2009 09:06:03 am Andreas Pakulat wrote:<br>
> On 06.03.09 08:40:17, Jim Bublitz wrote:<br>
> > On Friday 06 March 2009 06:53:01 am Marc Nations wrote:<br>
> > > Hi,<br>
> > > I'm trying to create a custom table which pops up a menu when the<br>
> > > user right clicks. This part works ok. It looks like this:<br>
> > ><br>
> > > class Table(QtGui.QTableWidget):<br>
> > > def __init__(self, parent, gui):<br>
> > > QtGui.QTableWidget.__init__(self, parent)<br>
> > > self.gui = gui<br>
> > ><br>
> > > def mouseReleaseEvent(self, event):<br>
> > > if event.button() == QtCore.Qt.RightButton:<br>
> > > self.rightClickMenu(event)<br>
> > ><br>
> > ><br>
> > > def rightClickMenu(self, event):<br>
> > > pos = event.pos<br>
> > > self.gui.ui.menuEdit.popup(QtGui.QCursor.pos())<br>
> > ><br>
> > ><br>
> > > The problem is that the default before of left click is changed,<br>
> > > and I can't reset it. Without the mods the left clicks acts where<br>
> > > if a multiple selection is made then clicking on another table<br>
> > > cell de-selects all the previous items (unless a modifier key is<br>
> > > used). With the above method, once multiple selections are made<br>
> > > then it basically goes into <shift> mode and all previous<br>
> > > selections are kept. I can't figure out a way to turn that off.<br>
> > ><br>
> > > Is there a way to cherry pick which mouse events you want and<br>
> > > ignore the rest, basically letting them keep their default<br>
> > > behavior? Because it looks like once the function is taken over<br>
> > > then the default behaviors are lost.<br>
> ><br>
> > Look at QWidget.contextMenuEvent - it catches only right clicks.<br>
> > Overload that.<br>
> ><br>
> > If you want to grab those on the vertical or horizontal headers, I<br>
> > think you'll have to install an event filter on the respective<br>
> > QHeaderViews (QObject.installEventFilter) and grab only<br>
> > QContextMenuEvents for those objects.<br>
><br>
> The header views are qwidgets as well, so I don't see why that would<br>
> be necessary.<br>
<br>
</div></div>Because you'd have to subclass them and replace the QTableWidget's<br>
original headers. It seems easier to install an event filter, but<br>
either method would work.<br>
<div class="im"><br>
> Apart from that, its even possible without overriding any base class,<br>
> using QWidget.setContextMenuPolicy (set to Qt.CustomContextMenu) and<br>
> catching the customContextMenuRequested() signal from the same<br>
> widget.<br>
<br>
</div>That's probably easier than installing an event filter - wasn't aware of<br>
that.<br>
<font color="#888888"><br>
Jim<br></font></blockquote><div><br></div><div><br></div><div>Got it working. Here's the code for posterity:</div><div><br></div><div><div><br></div></div><div><div> tableWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)</div>
<div> QtCore.QObject.connect(tableWidget, QtCore.SIGNAL("customContextMenuRequested(QPoint)"), self.contextMenu)</div><div> </div><div> def contextMenu(self, event):</div><div> self.gui.ui.menuEdit.popup(QtGui.QCursor.pos())</div>
<div><br></div><div><br></div><div>Deceptively simple, but solves both problems. I don't have to sub-class the Table Widget, and I can simply borrow a menu I created with Qt Designer and not have to add it all in manually. Awesome.</div>
<div><br></div><div>Thanks for all the help.</div></div><div></div></div>