Qt 上下文菜单破坏 QTreeView 中的选择

发布于 2024-07-06 22:11:26 字数 575 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南巷近海 2024-07-13 22:11:27

我尚未验证的一个可能的解决方案是捕获右键单击的单击事件,手动在树视图中进行选择,然后调用父级单击事件,该事件将依次激活上下文菜单。

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.

往昔成烟 2024-07-13 22:11:27

你没有说你使用的是哪个版本的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.

温折酒 2024-07-13 22:11:27

子类化 QTreeView 并添加受保护的方法 void contextMenuEvent(QContextMenuEvent *event); 在此方法中,您执行 QMenu:

class TreeView : public QTreeView{
  Q_OBJECT
public:
  TreeView(QWidget *parent);
  ~TreeView();
protected:
  void contextMenuEvent(QContextMenuEvent *event);
};

void TreeView::contextMenuEvent(QContextMenuEvent *event){
  QMenu menu(this);
  menu.addAction(action1);
  menu.addAction(action2);
  //...
  menu.addAction(actionN);
  menu.exec(event->globalPos());
}

Subclass the QTreeView and add the protected method void contextMenuEvent(QContextMenuEvent *event); In this method you execute a QMenu:

class TreeView : public QTreeView{
  Q_OBJECT
public:
  TreeView(QWidget *parent);
  ~TreeView();
protected:
  void contextMenuEvent(QContextMenuEvent *event);
};

void TreeView::contextMenuEvent(QContextMenuEvent *event){
  QMenu menu(this);
  menu.addAction(action1);
  menu.addAction(action2);
  //...
  menu.addAction(actionN);
  menu.exec(event->globalPos());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文