PDA

View Full Version : Easiest way to detect right mousebutton?



Teuniz
20th May 2007, 16:43
From the documentation:

void QListWidget::itemPressed ( QListWidgetItem * item ) [signal]

This signal is emitted with the specified item when a mouse button is pressed on an item in the widget.

See also itemClicked() and itemDoubleClicked().

I want to detect if the right mousebutton is pressed on a QListWidgetItem (in a QListWidget). How do I do this without catching all the mouse-events?

marcel
20th May 2007, 16:49
You can try using QWidget::customContextMenuRequested( const QPoint& ) and detect what item( if any ) is at that position.

This is always for the right mouse button.

Regards

Michiel
20th May 2007, 16:51
Subclass the QListWidget and redefine the QAbstractItemView::mousePressEvent() function. The QMouseEvent pointer that is passed can give you information about the button that was pressed and the position of the mouse-cursor (which you can translate to the right item).

About marcel's idea: I think that the context-menu can be called in more than one way (some keyboards have a key for it, for example). So you might want to go with the subclassing method.

marcel
20th May 2007, 16:55
About marcel's idea: I think that the context-menu can be called in more than one way (some keyboards have a key for it, for example). So you might want to go with the subclassing method.


But if a context menu key is pressed mousePressEvent won't work.
But the function I proposed will, since Qt will detect the key ( at least I assume it will ). But the correct way would be top detect it.

Regards

wysota
20th May 2007, 16:59
I want to detect if the right mousebutton is pressed on a QListWidgetItem (in a QListWidget). How do I do this without catching all the mouse-events?

What do you want to use that knowledge for?

Michiel
20th May 2007, 17:01
But if a context menu key is pressed mousePressEvent won't work.
But the function I proposed will, since Qt will detect the key ( at least I assume it will ). But the correct way would be top detect it.

Regards

Exactly my point.

Teuniz did not say he wanted to intercept the context-menu. He wanted to intercept the right mouse button. Or so he said in his post.

Teuniz
21st May 2007, 09:24
What do you want to use that knowledge for?
I made a small hobbyproject: http://www.teuniz.net/q3spy/
As you can see, there is a list of servers. When you dubbelclick with the
left mousebutton, the (external) game will start. Now I want to make it
possible to view/edit the properties of a server by rightclicking on a server
instead of navigating via the menu.

wysota
21st May 2007, 09:29
You have to reimplement mouseReleaseEvent then.