On Fri, Mar 6, 2009 at 12:05 PM, Jim Bublitz <jbubl...@nwinternet.com>wrote:
> - Show quoted text - > On Friday 06 March 2009 09:06:03 am Andreas Pakulat wrote: > > On 06.03.09 08:40:17, Jim Bublitz wrote: > > > On Friday 06 March 2009 06:53:01 am Marc Nations wrote: > > > > Hi, > > > > I'm trying to create a custom table which pops up a menu when the > > > > user right clicks. This part works ok. It looks like this: > > > > > > > > class Table(QtGui.QTableWidget): > > > > def __init__(self, parent, gui): > > > > QtGui.QTableWidget.__init__(self, parent) > > > > self.gui = gui > > > > > > > > def mouseReleaseEvent(self, event): > > > > if event.button() == QtCore.Qt.RightButton: > > > > self.rightClickMenu(event) > > > > > > > > > > > > def rightClickMenu(self, event): > > > > pos = event.pos > > > > self.gui.ui.menuEdit.popup(QtGui.QCursor.pos()) > > > > > > > > > > > > The problem is that the default before of left click is changed, > > > > and I can't reset it. Without the mods the left clicks acts where > > > > if a multiple selection is made then clicking on another table > > > > cell de-selects all the previous items (unless a modifier key is > > > > used). With the above method, once multiple selections are made > > > > then it basically goes into <shift> mode and all previous > > > > selections are kept. I can't figure out a way to turn that off. > > > > > > > > Is there a way to cherry pick which mouse events you want and > > > > ignore the rest, basically letting them keep their default > > > > behavior? Because it looks like once the function is taken over > > > > then the default behaviors are lost. > > > > > > Look at QWidget.contextMenuEvent - it catches only right clicks. > > > Overload that. > > > > > > If you want to grab those on the vertical or horizontal headers, I > > > think you'll have to install an event filter on the respective > > > QHeaderViews (QObject.installEventFilter) and grab only > > > QContextMenuEvents for those objects. > > > > The header views are qwidgets as well, so I don't see why that would > > be necessary. > > Because you'd have to subclass them and replace the QTableWidget's > original headers. It seems easier to install an event filter, but > either method would work. > > > Apart from that, its even possible without overriding any base class, > > using QWidget.setContextMenuPolicy (set to Qt.CustomContextMenu) and > > catching the customContextMenuRequested() signal from the same > > widget. > > That's probably easier than installing an event filter - wasn't aware of > that. > > Jim > Got it working. Here's the code for posterity: tableWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) QtCore.QObject.connect(tableWidget, QtCore.SIGNAL("customContextMenuRequested(QPoint)"), self.contextMenu) def contextMenu(self, event): self.gui.ui.menuEdit.popup(QtGui.QCursor.pos()) 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. Thanks for all the help.
_______________________________________________ PyQt mailing list PyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt