开发者

how to set a pop up menu on a particular table view item

i have a QTableView , and i need to s开发者_如何学Gohow a popup menu that shows the item properties . i need to set the context menu to apear only when you right click over a particular items in that tableview. but coudln't find a way to do it . i can set the context menu to appear when your over the table . i cant have it for each item . so how do i set the context menu over items in the tableview ?

please tell me if the idea was not clear enough

thanks in advance


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜