Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

tree view custom context menu for specific column



  • i have a tree view with a few columns. i created a custom header view and set it as tree header. implemented contextMenuEvent() of the header:

    class TreeHeader : public QHeaderView
    {
    	Q_OBJECT
    public:
    	TreeHeader(Qt::Orientation ori, QWidget *parent = nullptr);
    protected:
    	void contextMenuEvent(QContextMenuEvent *event) override;
    };
    

    the implementation:

    void TreeHeader::contextMenuEvent(QContextMenuEvent *event)
    {
    	QMenu contextMenu;
            // create and add actions
    	contextMenu.exec(event->globalPos());
    }
    

    this menu is shown for every column in the tree, but i need it only on the first column. how can i do that?


  • Lifetime Qt Champion

    Hi
    You can try to use
    https://doc.qt.io/qt-5/qheaderview.html#logicalIndexAt-2 to find the column
    and only show the menu if its the right one.


Log in to reply