Results 1 to 8 of 8

Thread: How to tell which tab widgets tab is requesting custom menu

  1. #1
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to tell which tab widgets tab is requesting custom menu

    Lets say I have a bunch of tabs on a tab widget.. I also have a custom context menu for tab widget but I want a different custom context menu to show depending on what tab the user right-clicks on.. How can I tell which tab the user is right-clicking on???

    eg, if the user right clicks on the "orders" tab, I want a custom context menu to drop down where they can add new order, delete order, ect.. If they right click on the "customers" tab, they can then add, change, delete customers..

    Let me know if more information is needed..

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to tell which tab widgets tab is requesting custom menu

    I think it would be easier to make apropriate context menu in widgets added to the tabs. For example: In orders tab you would have some OrdersWidget with some list of orders or something. Make the order's menu as a context menu in OrdersWidget. Then:
    Qt Code:
    1. tabWidget->addTab(new OrdersWidget, tr("Orders"));
    To copy to clipboard, switch view to plain text mode 
    So clicking anywhere on tab page will result in showing the orders menu. Same with CustomerWidget and so on.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. The following user says thank you to faldzip for this useful post:

    tgreaves (23rd March 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to tell which tab widgets tab is requesting custom menu

    Hmm.. Now that I think of it.. Wouldnt that happen as usual anyway?
    Meaning that if you right click on the tab of a widget(doesnt matter if its the active tab or not), tab(widget) would throw a signal asking for a custom context menu.. If thats true(i think it is), then the solution to my problem is already handled..

  5. #4
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to tell which tab widgets tab is requesting custom menu

    Just an fyi, im using slots and signals for the context menu..

    Qt Code:
    1. connect(ui.tabWidget_Orders, SIGNAL(customContextMenuRequested()), this, SLOT(slotDrawOrdersContextMenu));
    2. connect(ui.tabWidget_Customers, SIGNAL(customContextMenuRequested()), this, SLOT(slotDrawCustomersContextMenu));
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to tell which tab widgets tab is requesting custom menu

    I just did a test of this.. The main tab widget is called ui.tabWidget_Main.. I then added tabs to it, they are called ui.tabWidget_MainTab1 and ui.tabWidget_MainTab2..

    Problem is when I right click on either of the tabs, the main tab widget(ui.tabWidget_Main) is throwing the signal and not the other tabs.. Any ideas?

  7. #6
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to tell which tab widgets tab is requesting custom menu

    Spoke too soon.. The other tabs are throwing the custom context requested signal but the problem is I have to right click inside the widget itself.. The tab widget is throwing the custom context menu signal when I click on the tabs, which makes sense..

    Now, is there any way for the widgets to throw the signal when I right-click on the tab?

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to tell which tab widgets tab is requesting custom menu

    Not proved, but how to get the position of the mouse click event and calculate where it occurs.

  9. #8
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to tell which tab widgets tab is requesting custom menu

    hmm ok, I made something like this:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5. ui->tabWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    6. }
    To copy to clipboard, switch view to plain text mode 
    And:
    Qt Code:
    1. void MainWindow::on_tabWidget_customContextMenuRequested(const QPoint & pos)
    2. {
    3. QTabBar * tabBar = qobject_cast<QTabBar *>(ui->tabWidget->childAt(pos));
    4. if (!tabBar)
    5. return;
    6. QPoint pos2 = tabBar->mapFromParent(pos);
    7. int tabIndex = tabBar->tabAt(pos2);
    8. qDebug() << tabIndex;
    9. }
    To copy to clipboard, switch view to plain text mode 
    So the tabIndex contains index of tab if you click right mouse button on the tab bar of tab widget. Then you can show the menu appriopriate to the tab index. You can make some public function returning the menu from the widget set on tab page.
    Hmm maybe there's some easier solution but I dont know it :P
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Custom Widgets and layout managers...
    By TemporalBeing in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2009, 13:48
  2. Replies: 2
    Last Post: 16th May 2008, 14:39
  3. picking geometry in qt custom widgets
    By notsonerdysunny in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2007, 23:01
  4. create custom widgets
    By nimmyj in forum General Discussion
    Replies: 1
    Last Post: 20th November 2006, 08:24
  5. image for a custom menu Item.......
    By Naveen in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2006, 09:28

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.