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. Trouble getting QTabWidget context menu to work right

Trouble getting QTabWidget context menu to work right

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.3k 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.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      slavic_d
      wrote on last edited by
      #2

      Hello!
      I have run across a similar problem.
      Did you eventually solve yours?
      Best,
      slavic_d

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @slavic_d Hi and welcome to devnet,

        Take a look at the implementation in the Menus example.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

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