[SOLVED] QTabWidget remove tab problem!
-
No, there is no child widget for TabWidget !
The error occurs only when I remove the last tab. For example, suppose that
I have three tab in the TabWidget. When i remove tab 1, there is no error.
When i remove tab 2, there is no error. But the application crashes when i remove
tab 3. Also, when i call the method clear(), to remove all tabs I have the some error ! -
Hi,
I would say: you don't have a widget at that index, it will be 1. It works twice because you have 3 widgets.
A rule of thumb: never delete something that you havent check for its existence.
Hope it helps
-
Hi,
Even this code led to the some error :
@
connect(this, SIGNAL(tabCloseRequested(int)),
this, SLOT(closeTab(int)));void TabWidget::closeTab(int index)
{
removeTab(index);
}
@any suggestion ?
-
Use valgrind and check for memory leaks and null pointers.
You can also check your code by Cppcheck.
-
Is it possible at all to have a tab widget with zero tabs?
Maybe you just remove the tab widget altogether, or at least hide it, instead?
Also, what if you change your code to:
@void MainWindow::closeTab(int index)
{
if((index < 0) || (index >= m_parentTabWidget->count()))
{
qWarning("Oups!!!!");
return;
}
m_parentTabWidget->removeTab(index);
}@If that doesn't reveal anything, make a DEBUG build and see where exactly it crashes...
-
Hi friends,
bq. MuldeR wrote : Is it possible at all to have a tab widget with zero tabs?
What made me crazy, is the fact that initially the TabWidget doesn't contain any tab and the application starts perfectly. The class document does not mention this trick. So, I have thought to my web Browser! When I close the last opened tab the browser close itself automatically! So I have already changed the TabWidget like this :
@
void MainWindow::closeTab(int index)
{
if(m_parentTabWidget->count() == 1)
return;m_parentTabWidget->removeTab(index); }
@
thnk you :)
bq. Hostel wrote : Use valgrind and check for memory leaks and null pointers.
It would be a good tool !
Have you a good and simple quick-start other than the official one ?thanks. :)
-
I don't think you need necessarily need something like Valgrind here.
Just make a DEBUG build of your app, link it with the DEBUG Qt libraries and then run your app in a debugger.
As soon as it crashes, check the callstack...
--
Anyway, if you are on Linux and want to try Valgrind, then see here:
http://valgrind.org/docs/manual/quick-start.htmlThough this is more for finding memory leaks, uninitialized memory reads and stuff...
As long as your app does crash (reproducible), thing are usually straight forward in a "normal" debugger.
-
OK thanks. :)
-
Hello!
I have the same problem and found this thread.
Is there a better solution than just not deleting the last tab?
I have a class which is derived from QTabWidget. If I try to close the last tab (with "removeTab" or "clear"), the app crashes because of a segfault.
I dont think i handle some pointers wrong, because i can close every tab, as long as if there are more than just one.
My TabWidget starts with zero tabs, so why it is not possible to delete the last one?
Any suggestions?
Thank you. -
Hi there!
I know this thread is quite old, but I encountered a similar problem.
The issue for me was that I had registered a slot to handle the QTabWidget's 'currentChanged' signal.
When the last tab is closed in a QTabWidget, the 'currentChanged' signal is emitted with -1 as the argument
for the 'index' parameter, but the slot I had created to handle this signal was trying to access a tab (and no tabs exist
at this point).So, I simply added the line:
if (index < 0) return;
to the start of the slot, and voila! Problem solved!
I hope that helps someone :)