Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QContextMenu over which widget?
QtWS25 Last Chance

QContextMenu over which widget?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mirswith
    wrote on last edited by
    #1

    Is there a way to determine what widget is under the context menu? I want to adjust my context menu based on what widget is at the context position.

    I have tried variations of childAt but have not had any luck. For example if I am wondering if I am over a specific QTreeView I have tried things like:

    @
    void SomeClass::contextMenuEvent(QContextMenuEvent * event)
    {
    QWidget * p = static_cast<QWidget*>(_treeView->parent());
    QTreeView * tv = dynamic_cast<QTreeView*>( p->childAt( p->mapFromGlobal(event->globalPos()) ) );
    if( tv==_treeView ) // add items x,y & z to my context menu
    ...
    }
    @

    However "tv" is always NULL even if I trigger the context menu in the middle of my QTreeView.

    Any ideas? Thanks.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bodzio131
      wrote on last edited by
      #2

      As far as I know there is QWidget::contextMenuEvent virtual method. This way you can show context menu directly from your widget, without any additional recognition. You could for example inherit QTreeView and just implement contextMenuEvent to show customized context menu just for this particular widget.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mirswith
        wrote on last edited by
        #3

        That is true however I have some global menu items for that entire window but only want to show certain options when I am over certain widgets.

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zither
          wrote on last edited by
          #4

          set "contextMenuPolicy":http://developer.qt.nokia.com/doc/qt-4.7/qwidget.html#id-36e9c206-1ce6-47bd-b618-dacdfbbcf7b6
          @QWidget::setContextMenuPolicy(Qt::CustomContextMenu);@

          Then, u can trigger the custom Context Menu request by

          @connect(QWidget,SIGNAL(customContextMenuRequested ( const QPoint & pos )),this,SLOT(showYourMenu(const QPoint & pos)));@

          Cheers.. :)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mirswith
            wrote on last edited by
            #5

            Perfect, thanks!!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved