PDA

View Full Version : How to tell which tab widgets tab is requesting custom menu



tgreaves
23rd March 2009, 18:52
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..

faldzip
23rd March 2009, 19:14
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:


tabWidget->addTab(new OrdersWidget, tr("Orders"));

So clicking anywhere on tab page will result in showing the orders menu. Same with CustomerWidget and so on.

tgreaves
23rd March 2009, 19:22
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..

tgreaves
23rd March 2009, 19:26
Just an fyi, im using slots and signals for the context menu..



connect(ui.tabWidget_Orders, SIGNAL(customContextMenuRequested()), this, SLOT(slotDrawOrdersContextMenu));
connect(ui.tabWidget_Customers, SIGNAL(customContextMenuRequested()), this, SLOT(slotDrawCustomersContextMenu));

tgreaves
23rd March 2009, 19:58
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?

tgreaves
23rd March 2009, 20:10
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?

Lykurg
23rd March 2009, 20:48
Not proved, but how to get the position of the mouse click event and calculate where it occurs.

faldzip
23rd March 2009, 21:01
hmm ok, I made something like this:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
ui->tabWidget->setContextMenuPolicy(Qt::CustomContextMenu);
}

And:


void MainWindow::on_tabWidget_customContextMenuRequeste d(const QPoint & pos)
{
QTabBar * tabBar = qobject_cast<QTabBar *>(ui->tabWidget->childAt(pos));
if (!tabBar)
return;
QPoint pos2 = tabBar->mapFromParent(pos);
int tabIndex = tabBar->tabAt(pos2);
qDebug() << tabIndex;
}

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