PDA

View Full Version : Removing a tab in QTabWidget removes tabs to right as well



mboeni
19th October 2010, 09:57
Hi

I am using a QTabWidget and have implemented a method which reacts to the "oncloseRequest" signal (clicking on the close icon of a tab):


void generat0rX::sceneTabRemove_slot(int index)
{
sceneTabWidget->currentWidget()->deleteLater();
sceneTabWidget->removeTab(index);
}


My problem is that not only the active tab is deleted, but all to the right of it as well (having higher indexes than the currently active one).

Is there a fundamental mistake I am making in the method above?

Thanks & regards,
Michael

marcvanriet
19th October 2010, 11:45
Hi,

You could put a qDebug() << "tab index " << index << "removed" in this slot.

I guess it gets called recursively until there are no more tabs. You should see this in the debug output then.

Best regards,
Marc

mboeni
19th October 2010, 15:22
Hi Marc

I did as you proposed and can clarify the behaviour a bit (I found the pattern):
Whenever i close a tab, the one immediately to its left (current index - 1) is also deleted. This behaviour is constant and reproducible.

I also think I understood the problem: Both commands (removeTab, deleteLater) actually remove the tab! So first the tab is removed, then the one further down the index becomes current and is also removed (by the second command).

As I want to delete the widget (memory considerations) when it is closed, would "deleteLater()" be better? or is the object deleted automatically by using removeTab?

Cheers,
Michael

marcvanriet
20th October 2010, 12:20
Hi,

I'm sorry but I'm not sure. According to the documentation of removeTab, the widget itself is not deleted, so you have to take care of that yourself. If you do deleteLater() only, does it remove itself from the tabWidget ? The documentation doesn't say that the tabWidget becomes parent of your widget, so I wouldn't know how your widget can know that it is part of a tabwidget.

Maybe you could try :

QWidget *pWidget = sceneTabWidget->widget(index);
if( pWidget!=NULL)
{
sceneTabWidget->removeTab(index);
pWidget->deleteLater();
}


Best regards,
Marc

mboeni
20th October 2010, 14:29
Hi Marc

I currently use the following code:


void generat0rX::sceneTabRemove_slot(int index)
{
sceneTabWidget->currentWidget()->deleteLater();
}


And it indeed does remove the tab from the QTabWidget! Perhaps this is a glitch in the documentation.

As long as it works stable (or until someone ponts out that this is ultimately wrong) I think I'll leave it like this.

Does the above work for you too?

Cheers,
michael

marcvanriet
20th October 2010, 14:37
Hi,

I have no app where I do this, so I can't tell.
You could put a qDebug() << "widget deleted" in the destructor of your widget to see if the destructor gets called. If it does, I guess it is OK.

Best regards,
Marc

mboeni
20th October 2010, 15:17
Hi Marc

Yep, the widget gets deleted properly. looks right to me, then :)

Cheers,
Michael