我已使用以下代码在QTreeView项目中实现了上下文菜单
MyDerivedQTreeView->setModel(MyDerivedQAbstractItemModel);
MyDerivedQTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(MyDerivedQTreeView,
SIGNAL(customContextMenuRequested(const QPoint &)),
MyDerivedQAbstractItemModel(),
SLOT(contextualMenu(const QPoint &)));
void MyDerivedQAbstractItemModel::contextualMenu(const QPoint& point)
{
QMenu *menu = new QMenu;
menu->addAction(QString("Test Item"), this, SLOT(test_slot()));
menu->exec(MyDerivedQTreeView->mapToGlobal(point));
}
Run Code Online (Sandbox Code Playgroud)
调用MyDerivedQAbstractItemModel :: contextualMenu(),我可以看到上下文菜单.
问题是只有当用户右键单击某个项目并且应根据所选项目进行自定义时,才能看到上下文菜单.
如何从QPoint信息中选择/选择哪个项目?我在Qt 4.5.3.
| 归档时间: |
|
| 查看次数: |
11255 次 |
| 最近记录: |