how to set a pop up menu on a particular table view item
Assuming you're in control of when the menu pops up, then you'll want to use the indexAt(QPoint) member function in order to determine what item the mouse is over.
If you're not currently in control of when the menu shows up, you'll need to set the view's contextMenuPolicy to something that will give you control over it.
For example, if you subclass and override contextMenuEvent the implementation might look something like the following:
void MyView::contextMenuEvent ( QContextMenuEvent * event )
{
QModelIndex index = indexAt(event->pos());
if (index.data(Qt::UserRole + NEEDS_CONTEXT_MENU_ROLE_OFFSET).toBool())
// display context menu
else
// don't display context menu
}
You could also install an event handler to avoid subclassing.
精彩评论