Qt 上下文菜单破坏 QTreeView 中的选择
我有一个 QTreeView 类,其上下文菜单安装如下:
m_ui.tree->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_ui.tree, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(ShowTreeContextMenu(const QPoint&)));
...
void ShowTreeContextMenu(const QPoint& point)
{
m_treeContextMenu->exec(m_ui.tree->viewport()->mapToGlobal(point));
}
但是,当显示上下文菜单时,QTreeView 将不再响应鼠标单击。 显示上下文菜单时单击QTreeView
中的某个项目将删除上下文菜单,但不会选择单击的项目。
当右键单击新项目时,这尤其令人迷失方向,因为上下文菜单会在新项目上弹出,但由于未选择该项目,因此上下文菜单的内容引用了先前选择的项目。
I have a QTreeView
class with a context menu installed as follows:
m_ui.tree->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_ui.tree, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(ShowTreeContextMenu(const QPoint&)));
...
void ShowTreeContextMenu(const QPoint& point)
{
m_treeContextMenu->exec(m_ui.tree->viewport()->mapToGlobal(point));
}
However when the context menu is being displayed the QTreeView
will no longer respond to mouse clicks. Clicking on an item in the QTreeView
while the context menu is displayed will remove the context menu but does not select the clicked item.
This is especially disorientating when right clicking on a new item, as the context menu pops up over the new item, but as the item was not selected the contents of the context menu are referring to the previously selected item.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我尚未验证的一个可能的解决方案是捕获右键单击的单击事件,手动在树视图中进行选择,然后调用父级单击事件,该事件将依次激活上下文菜单。
A possible solution which I haven't verified would be to capture the click event of the right click, manually make the selection in the tree view and then invoking the parent click event which will in turn activate the context menu.
你没有说你使用的是哪个版本的Qt,但我们在Qt4.4.0中发现了同样的问题,它在4.3中有效。 我们将此作为错误225615 报告给
Trolltech仍标记为待处理,因此与此同时,我会遵循 Shy 的建议,拦截右键单击并自行进行选择。
You don't say which version of Qt you are using, but we found the same problem in Qt4.4.0, it worked in 4.3. We reported this to Trolltech as a bug 225615
This is still marked as pending, so in the meantime I would follow Shy's suggestion of intercepting the right click and making the selection yourself.
子类化 QTreeView 并添加受保护的方法 void contextMenuEvent(QContextMenuEvent *event); 在此方法中,您执行 QMenu:
Subclass the QTreeView and add the protected method void contextMenuEvent(QContextMenuEvent *event); In this method you execute a QMenu: