Trouble getting QTabWidget context menu to work right
-
Hello,
I have been trying to get the right-click context menu on my QTabWidget working. When I right click a tab, it calls launchTabWidgetContextMenu() which creates and executes the context menu. I have run into several problems. The first is that the context menu always appears on the top left corner of the screen when I really want it to appear where I right-clicked. The second problem is that I want to make the menu individual for each tab. That way when I trigger the actions they only occur for one tab, not all the tabs. Currently if I click "pin tab", all the tabs are pinned. I only want the tab I right clicked on to be pinned. The third problem lies in my closeTabAction. I have a function called closeTab() that works great when called by clicking the close button on the tabs, but when I click on the action it gives me an error
@QObject::connect: Incompatible sender/receiver arguments
QAction::triggered() --> mainView::closeTab(int)@here is my code:
@ tabWidget = new QTabWidget(this);
tabWidget->addTab(new browseTab(this, initialUrl), tr("Search"));
tabWidget->setTabsClosable(true);
tabWidget->setMovable(true);
tabWidget->setDocumentMode(true);
tabWidget->setContextMenuPolicy(Qt::CustomContextMenu);
tabWidget->setTabShape(QTabWidget::Rounded);
tabWidget->setTabPosition(QTabWidget::North);
tabWidget->setStyleSheet(
"QTabBar::tab { height: 25px; }"
"background-color: white;"
);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(closeTab(int)));
connect(tabWidget, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(launchTabWidgetContextMenu()));
@@void mainView::launchTabWidgetContextMenu()
{
QAction *pinTabAction = new QAction(this);
pinTabAction->setText("Pin Tab");
connect(pinTabAction, SIGNAL(triggered()), SLOT(pinTab()));QAction *closeTabAction = new QAction(this); closeTabAction->setText("Close Tab"); connect(closeTabAction, SIGNAL(triggered()), SLOT(closeTab(int))); QAction *insertTabAction = new QAction(this); insertTabAction->setText("Insert Tab"); QAction *disableTabDragAction = new QAction(this); disableTabDragAction->setText("Disable Tab Dragging"); QMenu *tabWidgetContextMenu = new QMenu; tabWidgetContextMenu->addAction(pinTabAction); tabWidgetContextMenu->addAction(closeTabAction); tabWidgetContextMenu->addAction(insertTabAction); tabWidgetContextMenu->addAction(disableTabDragAction); tabWidgetContextMenu->exec();
}@
@void mainView::pinTab()
{
tabWidget->setStyleSheet(
"QTabBar::tab { height: 25px; width: 35px; }"
"background-color: white;"
);
tabWidget->setTabShape(QTabWidget::Rounded);
}
@@void mainView::closeTab(int index)
{
tabWidget->removeTab(index);
}@Thank you!
-
@slavic_d Hi and welcome to devnet,
Take a look at the implementation in the Menus example.