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. How to display tab widget corner widget on the left/right side?

How to display tab widget corner widget on the left/right side?

Scheduled Pinned Locked Moved Solved General and Desktop
qtabwidgetqwidgetqt6.5.0qtabbar
2 Posts 1 Posters 2.2k 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.
  • C Offline
    C Offline
    CPPUIX
    wrote on 1 Jun 2023, 23:44 last edited by CPPUIX 6 Feb 2023, 12:10
    #1

    Hi,

    I have a QTabWidget with its tabs on the West, then I tried to add a corner widget to it, but it did not appear. If I set the tabs position to North or South, the corner widget gets displayed, but not on the side.

    Here's an MRE:

    QWidget *w = new QWidget();
    QTabWidget *t = new QTabWidget(w);
    
    w->setMinimumSize(800,600);
    
    t->addTab(new QWidget(),"Tab");
    t->addTab(new QWidget(),"Tab");
    t->addTab(new QWidget(),"Tab");
    t->addTab(new QWidget(),"Tab");
    
    t->setTabPosition(QTabWidget::TabPosition::East);
    
    t->setGeometry(100,100,400,400);
    
    QPushButton *button = new QPushButton("Button");
    
    //for debugging purposes
    button->setObjectName("ss");
    
    //for debugging purposes
    t->setStyleSheet("background: red");
    button->setStyleSheet("background: blue;");
    
    //for debugging purposes
    t->setCornerWidget(button1,Qt::TopLeftCorner);
    //t->setCornerWidget(button1,Qt::TopRightCorner);
    //t->setCornerWidget(button1,Qt::BottomLeftCorner);
    //t->setCornerWidget(button1,Qt::BottomRightCorner);
        
    w->show();
    
    //for debugging purposes
    //qDebug()<<button->geometry();
    //button->setGeometry(0,0,50,50);
    
    //for debugging purposes
    button->connect(button,&QPushButton::clicked,[](){qDebug()<<"corner widget click?";});
    

    I tried to use all 4 corners, and right corners have no effect, but the left causes an empty space before the tabs, here's how it looks, the empty space is at the top right corner:

    Offset before tabs

    I tried to set the stylesheet of the button I'm using as a corner widget, so that it might appear if hidden, but resulted in nothing.

    I checked the button's geometry, and I got QRect(0,0 0x0), and that is after using show(). So i tried to set its geometry, but resulted in nothing as well (but the geometry got updated correctly), button still not displayed.

    I also tried to check for the button whereabouts by connecting its clicked() signal to a qDebug(), I clicked all over the widget and got no output.

    QTabWidget::setCornerWidget:

    Note: Corner widgets are designed for North and South tab positions; other orientations are known to not work properly.

    I get from that, that it's not entirely impossible to get it to work.

    Is there a way to get a QTabWidget corner widget on the side?

    Thank you for your time and effort!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CPPUIX
      wrote on 3 Jun 2023, 11:35 last edited by
      #2

      From the Qt documentation (qt6), QTabWidget::setCornerWidget: says:

      Note: Corner widgets are designed for North and South tab positions; other orientations are known to not work properly.

      That means they do work, but are messy to and inconvenient to use.

      Here's a way to use QTabWidget corner widget on the side:

      As already noted in the question, setting a corner widget while tabs position is West or East, causes a small gap before the tabs without anything appearing there.

      But if you set QTabWidget's corner widget minimum size, it will appear, which solves a problem but causes another, because now I need to calculate that size myself, or make room for my corner widget.

      Here's an MRE that I used to try and figure how to get that empty corner size:

      QTabWidget *t = new QTabWidget();
      //I needed the stacked widget so I can use its geometry to calculate the empty corner size
      QStackedWidget *stack_widget = t->findChild<QStackedWidget*>("qt_tabwidget_stackedwidget");
      t->setMinimumSize(800,600);
      t->addTab(new QWidget(),"Tab1");
      t->addTab(new QWidget(),"Tab2");
      t->addTab(new QWidget(),"Tab3");
      t->addTab(new QWidget(),"Tab4");
      t->setTabPosition(QTabWidget::TabPosition::West);
      
      QToolButton *button1 = new QToolButton();
      button1->setIcon(QIcon(":/icons/collapse.png"));
      
      t->setCornerWidget(button1,Qt::TopLeftCorner);
      
      t->show();
      
      //width is equal to where the stack widget starts (x coordinate)
      //height is equal to where the tab bar starts (y coordinate)
      //I subtracted 1 from stackwidget's x because it simply looked better
      t->cornerWidget(Qt::TopLeftCorner)->setMinimumSize(stack_widget->geometry().x()-1,
                                                         t->tabBar()->geometry().y());
      
      //checking related widgets geometries
      /*qDebug()<<"cornerWidget geo"<<t->cornerWidget(Qt::TopLeftCorner)->geometry();
      qDebug()<<"tabBar rect"<<t->tabBar()->tabRect(0);
      qDebug()<<"tabBar geo"<<t->tabBar()->geometry();
      qDebug()<<"stackwidget geo"<<sw->geometry();*/
      

      Here's how it looks, I used a custom icon:

      Corner widget appeared

      If you need more space for the corner widget, you'll need to move the tab bar, because corner widget will cover it, you can do that using stylesheet. See this: Qt Style Sheets Examples: Customizing QTabWidget and QTabBar.

      Here's an example of stylesheet:

      t->setStyleSheet("QTabWidget::tab-bar "
                       "{"
                           "top: 50px;" /* push down by 50px */
                       "}");
      

      Here's how it looks with that being the only addition and change to my MRE:

      Extended tab widget corner widget

      Suggestion: QTabWidget::paintEvent might be a better solution.

      1 Reply Last reply
      1
      • C CPPUIX has marked this topic as solved on 3 Jun 2023, 16:12
      • P Paddle referenced this topic on 7 Nov 2023, 08:41

      2/2

      3 Jun 2023, 11:35

      • Login

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