Results 1 to 6 of 6

Thread: QTabBar - Context menu on tab

  1. #1
    Join Date
    Feb 2008
    Posts
    40
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTabBar - Context menu on tab

    I would like to show a context menu, when someone clicks on the tab. It's easy - just intercept signal customContextMenuEvent. But I have no idea, how to get proper tab just from the position of a click and not pop up a context menu, when user clicks somewhere else, than on a tab. Could someone help me on this?

  2. #2
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTabBar - Context menu on tab

    Here is what I did here http://code.google.com/p/qtedit4/sou...itabwidget.cpp

    Qt Code:
    1. bool qmdiTabWidget::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (obj != tabBar())
    4. return QObject::eventFilter(obj, event);
    5.  
    6. if (event->type() != QEvent::MouseButtonPress)
    7. return QObject::eventFilter(obj, event);
    8.  
    9. // compute the tab number
    10. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
    11. QPoint position = mouseEvent->pos();
    12. int c = tabBar()->count();
    13. int clickedItem = -1;
    14.  
    15. for (int i=0; i<c; i++)
    16. {
    17. if ( tabBar()->tabRect(i).contains( position ) )
    18. {
    19. clickedItem = i;
    20. break;
    21. }
    22. }
    23.  
    24. // just in case
    25. if (clickedItem == -1)
    26. return QObject::eventFilter(obj, event);
    27.  
    28. switch( mouseEvent->button() )
    29. {
    30. case Qt::LeftButton:
    31. return QObject::eventFilter(obj, event);
    32. break;
    33.  
    34. case Qt::RightButton:
    35. on_rightMouse_pressed( clickedItem, position );
    36. break;
    37.  
    38. case Qt::MidButton:
    39. on_middleMouse_pressed( clickedItem, position );
    40. break;
    41.  
    42. default:
    43. return QObject::eventFilter(obj, event);
    44. }
    45.  
    46. return true;
    47. }
    To copy to clipboard, switch view to plain text mode 

    I assume I would use a binary approach to reduce the time from O(n) to O(log(n)) [where n is the number of tabs), but in such magnitudes this optimization is irrelevant IMHO.

  3. #3
    Join Date
    Feb 2008
    Posts
    40
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTabBar - Context menu on tab

    That seems to be exactly what I need. Thanks a lot, love it

    And yeah, no point in complicated the code with useless optimisation. This is just fine, so I'll just translate it to Python

  4. #4
    Join Date
    Feb 2008
    Posts
    40
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTabBar - Context menu on tab

    One more thing.

    Qt Code:
    1. if (obj != tabBar())
    2. return QObject::eventFilter(obj, event);
    To copy to clipboard, switch view to plain text mode 

    Any time I click on the tab, it cannot pass through this condition. obj is QTabWidget (a class inheriting from this QTabWidget) instead of tabBar(). Do you have any idea, why it is so? From what I have read about your project, you are using Qt4. Does require doing something more?

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabBar - Context menu on tab

    look at this example
    Qt Code:
    1. ...
    2. m_tabBar = new QTabBar();
    3. m_tabBar->addTab(tr("OK"));
    4. m_tabBar->addTab(tr("NO"));
    5. m_tabBar->addTab(tr("IGNORE"));
    6. m_tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
    7.  
    8. connect(m_tabBar, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
    9. ...
    10.  
    11. void Test::showContextMenu(const QPoint &point)
    12. {
    13. if (point.isNull())
    14. return;
    15.  
    16. int tabIndex = m_tabBar->tabAt(point);
    17. QMenu menu(this);
    18. if (!tabIndex)
    19. menu.addAction(tr("OK"));
    20. else if (tabIndex == 1)
    21. menu.addAction(tr("NO"));
    22. else if (tabIndex == 2)
    23. menu.addAction(tr("IGNORE"));
    24.  
    25. menu.exec(m_tabBar->mapToGlobal(point));
    26. }
    To copy to clipboard, switch view to plain text mode 

    works perfectly.

  6. #6
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTabBar - Context menu on tab

    Thanks a lot, @spirit. Works like a charm!

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 10:52
  2. Context Menu
    By RY in forum Newbie
    Replies: 1
    Last Post: 10th September 2008, 08:59
  3. QTable context menu
    By grabnerj in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2008, 06:20
  4. context menu problem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2008, 14:18
  5. Replies: 4
    Last Post: 25th June 2007, 21:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.