PDA

View Full Version : Removing Tabs



NewGuy
22nd July 2006, 10:17
I'm having some problems with removing tabs from QTabWidget. I have this QTabWidget with a main tab, named (surprise!) mainTab. At runtime the user can open several new tabs to view text documents. In order to close these tabs again I have provided a push button, cmdClear. The code for the button is:


void QtTextSearcher::on_cmdClear_clicked()
{
int count = ui.mainTab->count();
int i = 1;
for (i=1; i<count; ++i)
{
ui.mainTab->widget(i)->deleteLater();
ui.mainTab->removeTab(i);
}
ui.txtOutput->clear();
ui.txtLog->append("Cleared");
}

When I press the buttton it doesn't delete all the document tabs however, it seems to always forget some. How come? I want it to delete all the tabs save the first mainTab.

Also: How can I position my QTabWidget like in the Assistant? In the Assistant the Tabwidgets are touching the toolbar, in designer I can't seem to get my Tabwidget anywhere near the edges of the mainwindow.

wysota
22nd July 2006, 11:24
Tab indexes start from 0 and not from 1, so your for is invalid.

NewGuy
22nd July 2006, 12:05
But tab index = 0 is the first tab right? I don't want to remove that, just the remaining tabs

wysota
22nd July 2006, 13:32
Also: How can I position my QTabWidget like in the Assistant? In the Assistant the Tabwidgets are touching the toolbar, in designer I can't seem to get my Tabwidget anywhere near the edges of the mainwindow.

Tweak the margins and spacing properties of the layout - set them to 0.

About your tab problem... your code seems fine altough I wouldn't use deleteLater() just simply delete. And do that after removing the widget from the tab widget. It's possible that deleting will suffice and you won't have to remove the widget from the tab widget. If that doesn't help, could you try to reproduce the problem using a minimal compilable example?

elcuco
22nd July 2006, 18:25
see the code of assistant, they use some ugly tricks use the code.

jpn
22nd July 2006, 18:34
I'm having some problems with removing tabs from QTabWidget.
...
When I press the buttton it doesn't delete all the document tabs however, it seems to always forget some. How come? I want it to delete all the tabs save the first mainTab.
Maybe you should remove the tabs from the end to the beginning. Each removeTab() reduces the count of the tabs, so the count isn't anymore the same than in the beginning of the loop.



// "greater than zero" makes sure that the tab with index zero is left
for (int i = count; i > 0; --i)
{
QWidget* tab = ui.mainTab->widget(i);
ui.mainTab->removeTab(i);
delete tab;
}

NewGuy
22nd July 2006, 22:46
Great - that works just fine.

About the position of my Tabwidget: When i force the widget into place by changing the geometry property it works just fine. When I resize the window it isn't resized with it however. If I try to make a layout with my mainwindow in order to fix this the tabwidget gets reset to a new position.
Hope I have described the problem clear enough