Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8684413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:18:05+00:00 2026-06-12T22:18:05+00:00

I was able to create a context menu for my QTreeWidget as below QMenu*

  • 0

I was able to create a context menu for my QTreeWidget as below

QMenu* pContextMenu = new QMenu(this)
QTreeWidget* pTreeWidget = new QTreeWidget();
QAction* pOpenFile = new QAction(tr("Open A File"), pContextMenu);
pTreeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
pTreeWidget->addAction(pOpenFile);

But I want a different popup for a branch than a leaf. How do I assign a different popup depending on the type of widgetitem clicked?

My tree:

  • Branch1 <– Popup1
    • Leaf1
    • Leaf2 <– Popup2
  • Branch2
    • Branch3
      • Leaf1

QWidget::actions() is not listed as virtual. Else I would have derived my own class from QTreeWidget & reimplemented actions().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:18:06+00:00Added an answer on June 12, 2026 at 10:18 pm

    Method 1: Override QTreeWidget

    A context menu assigned to the QTreeWidget itself will not let you have different context menus for different items, as you have discovered.

    As the Qt item views don’t have special API for context menus, you have to implement this yourself. Fortunately, it’s not very difficult; you just need to:

    1. Create a subclass of QTreeWidget.
    2. Connect the customContextMenuRequested(const QPoint&) signal to a custom slot.
    3. Display the desired context menu.

    I’ve posted a complete working example. Some details to note include:

    • QTreeWidgetItem provides a handy type property to let you identify items easily without casting, string parsing, or other awkward/fragile methods.

    • Custom QTreeWidgetItem type values should be greater than or equal to QTreeWidgetItem::UserType.

    • When displaying a context menu, you must pass a global position to exec(). To correctly map from a position in the widget’s space in the slot, you must use the item’s viewport widget.


    Method 2: Override QItemDelegate (and QTreeWidget …)

    An alternate method is to implement your own QAbstractItemDelegate subclass, and assign it to your tree widget. In your item delegate, you can override editorEvent() to handle mouse presses in the same way.

    Although this approach frres is actually more in line with Qt’s item view API design, there are a few key disadvantages to this approach:

    • Item delegates use QModelIndex objects to represent items. To convert to a QTreeWidgetItem, you must use the QTreeWidget::itemFromIndex() method. Unfortunately, this is protected, so it will actually require you to subclass QTreeWidget anyway to provide this API for your delegate. This adds some more boilerplate complexity to your code.

    • The editorEvent() hook is invoked before the item view handles the event. This means that you can’t easily display a context menu and allow the default behavior at the same time (such as selecting the item that was right-clicked).

    • Since the editorEvent() handler sees all kinds of different events, you must be even more careful to handle them correctly. You must also be careful not to let this monolithic handler grow out of control if your behaviors are complicated.

    The core code is very similar, but again, there’s a bit more boilerplate. I’ve posted an example of this approach, as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a context menu which will be able to add extra
I'm trying to create a context menu which will be able to add extra
I want to be able to create a file from the Content Provider, however
I'm able to create html links the following way: <a href=/about>About Us</a> This will
I'm able to create an excel file using apache poi. however, i want users
Is it possible to create a dynamic context menu in android? What I mean
I'm trying to create a right-click context menu command for compressing JavaScript files with
I'd like to create a custom context menu. The idea is to create a
I'm not able to add context menu entries for website project items. How to
I was able to create a tree view [using EXTJS 4 ] with context

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.