Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't QTabBar's tabBarDoubleClicked(int) handle double clicking the tab bar?

Tags:

c++

tabs

qt

This is driving me nuts. QTabBar's documentation says that:

void QTabBar::tabBarDoubleClicked(int index) [signal]

This signal is emitted when the user double clicks on a tab at index. index refers to the tab clicked, or -1 if no tab is under the cursor.

So, when I double click the tab bar, shouldn't it be returning -1?

Just to be clear, the bit in the red box is where I am trying to double click, and where I think it should be returning -1. It returns the tab index when I double click a tab, so I know that it is working correctly. ApplicationWindow

But shouldn't the bit in the red box still technically be the tab bar? Or does the tab bar expand as tabs are added? If so, is there a way to make it expand to fill the window horizontally?

I'm trying to implement adding a tab on double clicking the tab bar; is there another way I should be going about this?

like image 765
Thomas Avatar asked Oct 31 '22 16:10

Thomas


1 Answers

Run this code and add some bar.

void MainWindow::on_tabWidget_tabBarDoubleClicked(int index)
{
    qDebug() << index << ui->tabWidget->tabBar()->geometry();
}

You will see something like this:

0 QRect(0,0 288x29) 
2 QRect(0,0 288x29) 
("G:/x.txt", "G:/xx.txt", "") //something was added
3 QRect(0,0 311x29) //width increased
5 QRect(0,0 311x29) 
4 QRect(0,0 311x29) 

As you can width now is 311x29. And try to use this:

void MainWindow::on_tabWidget_tabBarDoubleClicked(int index)
{
    ui->tabWidget->removeTab(index);
    qDebug() << index << ui->tabWidget->tabBar()->geometry();
}

Result can be something like this:

2 QRect(0,0 221x29) 
2 QRect(0,0 154x29) 
1 QRect(0,0 50x21) 
0 QRect(0,0 0x0) 

As you can see, I delete tabs and TabBar becomes smaller. TabBar was resized automaticaly. Your area in red box isn't TabBar

To add some tab, you can provide special button or use tabBarDoubleClicked signal too, but use count() method to know, how much tabs are in your widget right now.

Edit:

For example:

void MainWindow::on_tabWidget_tabBarDoubleClicked(int index)
{
    int height = ui->tabWidget->tabBar()->height();
    ui->tabWidget->tabBar()->setGeometry(0,0,ui->tabWidget->geometry().width(), height);
    qDebug() << index << ui->tabWidget->tabBar()->geometry();
}

Of course you should setGeometry in another place (in constructor maybe), but I did this to do smaller code. Now tabBar is bigger, but there is no any changes in design or something else. Result:

2 QRect(0,0 311x29) 
1 QRect(0,0 311x29) 
-1 QRect(0,0 311x29) 
-1 QRect(0,0 311x29) 

As you can see, -1 appears in output, it appears when I click on area where there is no any tab(without setGeometry it is just empty area), so know you can catch this signal. When your index equals -1, you can create new tab(or open some dialog to let the user do some settings). I think it is all what you need.

Another way:

QPushButton *m_addButton = new QPushButton("+", this);
QPushButton *m_addButton1 = new QPushButton("-", this);
m_addButton->resize(15,15);
m_addButton1->resize(15,15);
ui->tabWidget->tabBar()->setTabButton(0, QTabBar::RightSide, m_addButton);
ui->tabWidget->tabBar()->setTabButton(0, QTabBar::LeftSide, m_addButton1);

Result:

enter image description here

like image 103
Kosovan Avatar answered Nov 15 '22 03:11

Kosovan