I have a QTreeView
class with a context menu installed as follows:
我有一个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));
}
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.
但是,当显示上下文菜单时,QTreeView将不再响应鼠标单击。在显示上下文菜单时单击QTreeView中的项目将删除上下文菜单,但不会选择单击的项目。
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.
当右键单击新项目时,这尤其迷失方向,因为上下文菜单弹出新项目,但由于未选择项目,上下文菜单的内容指的是先前选择的项目。
3 个解决方案
#1
1
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
你没有说你正在使用哪个版本的Qt,但我们在Qt4.4.0中发现了同样的问题,它在4.3中工作。我们向Trolltech报告了这个错误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.
这仍然标记为待定,因此在此期间我会按照Shy的建议截断右键并自行进行选择。
#2
2
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.
我尚未验证的可能解决方案是捕获右键单击的单击事件,在树视图中手动进行选择,然后调用父单击事件,该事件将依次激活上下文菜单。
#3
1
Subclass the QTreeView and add the protected method void contextMenuEvent(QContextMenuEvent *event); In this method you execute a QMenu:
子类化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());
}
#1
1
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
你没有说你正在使用哪个版本的Qt,但我们在Qt4.4.0中发现了同样的问题,它在4.3中工作。我们向Trolltech报告了这个错误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.
这仍然标记为待定,因此在此期间我会按照Shy的建议截断右键并自行进行选择。
#2
2
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.
我尚未验证的可能解决方案是捕获右键单击的单击事件,在树视图中手动进行选择,然后调用父单击事件,该事件将依次激活上下文菜单。
#3
1
Subclass the QTreeView and add the protected method void contextMenuEvent(QContextMenuEvent *event); In this method you execute a QMenu:
子类化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());
}