Qt World Summit: Register Today!

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
    	TreeHeader(Qt::Orientation ori, QWidget *parent = nullptr);
    	void contextMenuEvent(QContextMenuEvent *event) override;

    the implementation:

    void TreeHeader::contextMenuEvent(QContextMenuEvent *event)
    	QMenu contextMenu;
            // create and add actions

    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

    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