PDA

View Full Version : open context menu unde the items of a QListWidget



Dark_Tower
9th April 2006, 11:06
Hi all, I have a QListWidget where I show as icons the images in a specified directory. I want that when the user presses the right button of the mouse under an item of the list, it selects it and also opens a context menu with actions to work with the item or just opens the context menu if the selection (of one or more items) already exists. Anybody knows what's the simplest way to implement this behaviour? Thanks.

jpn
9th April 2006, 11:12
Check the second post in this thread:
http://www.qtcentre.org/forum/showthread.php?t=1710

Dark_Tower
9th April 2006, 11:17
Should I have to re-implement "mousePressEvent" in the list to catch when the user presses the right button or there's another way to make it automaticly?

Edit: I answer myself, I have to use QContextMenuEvent... ;)

Dark_Tower
12th April 2006, 17:58
Hi again. I still have a question about the context menu: is it strictly necesary to subclass a widget to handle context menus with it? I think that a context menu should be something that you can manage from "outside" the class... Can anybody explain me if it's possible and how to handle the context menus of a widget without having to subclass it to catch the QContextMenuEvent?

Dark_Tower
14th April 2006, 00:14
Any comments, please? :crying:

jacek
14th April 2006, 00:34
Can anybody explain me if it's possible and how to handle the context menus of a widget without having to subclass it to catch the QContextMenuEvent?
Open Qt Assistant, select the "Index" tab, enter "QListWidget" in the "Look for" text edit, select QListWidget from the list, click "List of all members, including inherited members" link, press Ctrl+F, enter "contextMenu" in the dialog and press return 5 times. I hope you will know what to do next.

Dark_Tower
14th April 2006, 01:05
Open Qt Assistant, select the "Index" tab, enter "QListWidget" in the "Look for" text edit, select QListWidget from the list, click "List of all members, including inherited members" link, press Ctrl+F, enter "contextMenu" in the dialog and press return 5 times. I hope you will know what to do next.

I think that I could find the treasure in the island following this instructions but all I've found is the "contextMenuEvent" that's a protected method so I should need to subclass QListWidget to use it and that's exactly what I want to avoid, if it's possible...

jacek
14th April 2006, 01:08
but all I've found is the "contextMenuEvent" that's a protected method
Then you should look harder next time.


void QWidget::customContextMenuRequested ( const QPoint & pos ) [signal]
This signal is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu, and the user has requested a context menu on the widget. The position pos is the position of the context menu event that the widget receives. Normally this is in widget coordinates. The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport() .
See also mapToGlobal(), QMenu, and contextMenuPolicy.
And there are event filters (http://doc.trolltech.com/4.1/qobject.html#installEventFilter) too.

Dark_Tower
14th April 2006, 01:13
Sorry, I've repeated the steps and finally I've found the treasure: customContextMenuRequested :D Thanks and sorry jacek

jpn
17th April 2006, 20:32
QWidget::contextMenuPolicy (http://doc.trolltech.com/4.1/qwidget.html#contextMenuPolicy-prop)

Qt::DefaultContextMenu -> override/catch contextMenuEvent
Qt::ActionsContextMenu -> a context menu containing all actions added to a widget (http://doc.trolltech.com/4.1/qwidget.html#addAction) is constructed and shown automatically
Qt::CustomContextMenu -> widget emits signal: customContextMenuRequested()

The latter 2 policies allow you to play with widget's context menus without subclassing or filtering events. For the first one, subclassing or an event filter is needed.