在 QTreeWidget 中调用上下文菜单

发布于 2024-08-11 22:30:45 字数 136 浏览 5 评论 0原文

当用户单击 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 技术交流群。

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

发布评论

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

评论(3

你是年少的欢喜 2024-08-18 22:30:45

编写您自己的自定义 ItemDelegate 并在 QAbstractItemDelegate::editorEvent 中处理点击事件。
您可以从 QModelIndex 检索单元格中的数据。
在 C++ 中,它看起来像这样:

class ItemDelegate: public QItemDelegate
{
public:
    ItemDelegate(ContextMenuHandler *const contextMenu, QObject *const parent )
        : QItemDelegate(parent)
        , m_contexMenu(contextMenu) 
    {
    }

    bool editorEvent( 
            QEvent * event, 
            QAbstractItemModel * model, 
            const QStyleOptionViewItem & option, 
            const QModelIndex & index )
    {
        if((event->type()==QEvent::MouseButtonPress) && index.isValid())
        {
            QMouseEvent *const mouseEvent = qobject_cast<QMouseEvent>(event);
            if(mouseEvent && (mouseEvent->button()==Qt::RightButton))
            {
                return m_contexMenu->showContextMenu(mouseEvent->pos(), index);
            }
        }
    }
    ContextMenuHandler *const m_contextMenu;
};

treeWidget->setItemDelegate(new ItemDelegate(contextMenuHandler,treeWidget));

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:

class ItemDelegate: public QItemDelegate
{
public:
    ItemDelegate(ContextMenuHandler *const contextMenu, QObject *const parent )
        : QItemDelegate(parent)
        , m_contexMenu(contextMenu) 
    {
    }

    bool editorEvent( 
            QEvent * event, 
            QAbstractItemModel * model, 
            const QStyleOptionViewItem & option, 
            const QModelIndex & index )
    {
        if((event->type()==QEvent::MouseButtonPress) && index.isValid())
        {
            QMouseEvent *const mouseEvent = qobject_cast<QMouseEvent>(event);
            if(mouseEvent && (mouseEvent->button()==Qt::RightButton))
            {
                return m_contexMenu->showContextMenu(mouseEvent->pos(), index);
            }
        }
    }
    ContextMenuHandler *const m_contextMenu;
};

treeWidget->setItemDelegate(new ItemDelegate(contextMenuHandler,treeWidget));
彼岸花ソ最美的依靠 2024-08-18 22:30:45

我正在使用这样的东西:

self.widget_layers.setContextMenuPolicy(Qt.ActionsContextMenu)
removeLayerAction = QAction("Remove selected layer", self)
self.connect(removeLayerAction, SIGNAL('triggered()'), self.layers_widget_controller.remove_selected_layer)

并检查哪个项目触发了信号:

selected_item = self.main_window.widget_layers.selectedItems()[0]

I'm using something like this:

self.widget_layers.setContextMenuPolicy(Qt.ActionsContextMenu)
removeLayerAction = QAction("Remove selected layer", self)
self.connect(removeLayerAction, SIGNAL('triggered()'), self.layers_widget_controller.remove_selected_layer)

and check which item triggered the signal by:

selected_item = self.main_window.widget_layers.selectedItems()[0]
七婞 2024-08-18 22:30:45

我对新信号/槽样式所做的操作:

self.treeMenu = QMenu()
self.treeAction = QAction('print', self.treeMenu)
self.treeAction.triggered.connect(self.printTreeItem)
self.treeWidget.addAction(self.treeAction)

@pyqtSlot()    
def printTreeItem(self):
    print self.treeWidget.currentItem().text(0)

当您在树形小部件中右键单击时,这将打开一个菜单。如果您单击“打印”,在控制台中它将打印出具有当前焦点的项目,即您右键单击的项目。

注意:当前项不一定是所选项,所选项是您最近单击的项。

What I did with the new signal/slot style:

self.treeMenu = QMenu()
self.treeAction = QAction('print', self.treeMenu)
self.treeAction.triggered.connect(self.printTreeItem)
self.treeWidget.addAction(self.treeAction)

@pyqtSlot()    
def printTreeItem(self):
    print self.treeWidget.currentItem().text(0)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文