PDA

View Full Version : different pop up menus wanted...



nupul
8th April 2006, 09:39
In Browsers, when you right-click on the web page background (i.e. white space) you get a Context menu, different from what you'd get on a text selection and again different from what you get on a "link" selection...I saw the same in Qt assistant too!

:eek:

How is this achieved in Qt4? Can the same also be applied to the items in QListWidget?

Thanks

Nupul

jpn
8th April 2006, 10:21
Which ever way you are handling the context menu (see context menu policy (http://doc.trolltech.com/4.1/qwidget.html#contextMenuPolicy-prop)), the QContextMenuEvent (or the QPoint in case of custom context menu) parameter contains the requested position.
QListWidget provides you methods (QListWidget::itemAt() (http://doc.trolltech.com/4.1/qlistwidget.html#itemAt)) which make it straightforward to check whether there is an item under the requested position or not.
You'll just need to construct and show the appropriate context menu based on the position.

rooney
27th June 2006, 01:04
Hi, following on from this, can someone please tell me how to use itemAt()?

In my case, I want the context menu to appear only when a TreeWidgetItem from a particular TreeWidget is right clicked. Therefore I have set the context menu policy accordingly, but I don't know how to use itemAt to perform the check...


QListWidget provides you methods (QListWidget::itemAt() (http://doc.trolltech.com/4.1/qlistwidget.html#itemAt)) which make it straightforward to check whether there is an item under the requested position or not.
You'll just need to construct and show the appropriate context menu based on the position.

Really appreciate your help on this ;)

jpn
27th June 2006, 06:21
// Qt::DefaultContextMenu
void MyTreeWidget::contextMenuEvent(QContextMenuEvent* event)
{
QTreeWidgetItem* item = itemAt(event->pos());
if (item)
{
// an item exists under the requested position

QMenu menu(this);
// ...
menu.exec(event->globalPos());
}
else
{
// there is no item under the requested position
}
}

john_god
10th February 2009, 22:52
Is there a equivalent to itemAt for the QTableWidget ??????

Ginsengelf
11th February 2009, 08:04
Err...qtablewidget.html#itemAt?