在 QTreeWidget 中调用上下文菜单
当用户单击 QTreeWidgetItem 中的对象时,我想弹出一个菜单。我想从 QWidget 捕获信号 contextMenuRequested ,然后使用 itemAt 从视图中检索索引。但这看起来不太漂亮。有没有更简单的方法可以调用视图内项目的菜单?
I would like to popup a menu, when user clicks on an object in QTreeWidgetItem. I though about catching signal contextMenuRequested from QWidget and then retrieving index from the view using itemAt. But this doesn't seem very pretty. Is there any easier way to be able to call a menu on an item inside a view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编写您自己的自定义 ItemDelegate 并在 QAbstractItemDelegate::editorEvent 中处理点击事件。
您可以从 QModelIndex 检索单元格中的数据。
在 C++ 中,它看起来像这样:
Write your own custom ItemDelegate and handle the click event in
QAbstractItemDelegate::editorEvent
.You can retreive the data in the cell from the QModelIndex.
In C++ it would look like this:
我正在使用这样的东西:
并检查哪个项目触发了信号:
I'm using something like this:
and check which item triggered the signal by:
我对新信号/槽样式所做的操作:
当您在树形小部件中右键单击时,这将打开一个菜单。如果您单击“打印”,在控制台中它将打印出具有当前焦点的项目,即您右键单击的项目。
注意:当前项不一定是所选项,所选项是您最近单击的项。
What I did with the new signal/slot style:
This will open a menu when you right-click within the your treeWidget. And if you click the 'print', in your console it will print out the item which has the current focus, it's the one that you right-clicked.
Note: the current item is not necessary the selected item, the selected item is the one you most recently clicked.