Solved Cannot delete items of a QVBoxLayout
-
@mrjj
Thanks, I tried and it crashes at exactely this line you gave me.
I can not do anything with the layout
Seems to be a nasty one... -
@Niagarer
If it crashes there, something must be wrong with the pointer
myBoxLayout->count();You did show real code, yes ?
A classic is to define in .h
and then do
QVBoxLayout *myBoxLayout = new QVBoxLayout; ( in ctror)
and make local variable instead.set it to null where u define it
QVBoxLayout *myBoxLayout=nullptr;and check if its NON null before using it on the count()
-
@mrjj
Ok, I tried it.
I did not make the classic mistake but if I define it with a nullpointermyBoxLayout = nullptr;
and then asking if it's nullptr where I try to delete all content, it says, it is not null
But I only defined it once...
And if I ask that this way:myBoxLayout = nullptr; if(myBoxLayout != nullptr){ qDebug() << "layout is not null"; }
it says, it is not null...
-
@Niagarer
what ??
myBoxLayout = nullptr;
if(myBoxLayout != nullptr){
qDebug() << "layout is not null";
}that code should say nothing. :)
I assume that
myBoxLayout = nullptr; <<< in .h yes ?Sorry, i have really no idea.
Its does work that way and i have used takeAt many times. -
@mrjj
In the .h file:QVBoxLayout *myBoxLayout;
and in the .cpp file:
myBoxLayout = nullptr; if(myBoxLayout == nullptr){ qDebug() << "layout is null"; }
and it prints nothing...
I really don't understand that too.
I just hope that I am not doing a totally dumb mistake here... -
@Niagarer
Hi, no worries, we all do stupid mistakes sometimes.
Its a sign of a productive programmer ;)first
QVBoxLayout *myBoxLayout; -> QVBoxLayout *myBoxLayout=nullptr.
in .h
then do NOT
myBoxLayout = nullptr; before the check. ( you kill it )also , it must print something ???
myBoxLayout = nullptr;
if(myBoxLayout == nullptr){
qDebug() << "layout is null";
}
else something is really, really wrong.Can you test this small sample ?
https://www.dropbox.com/s/tkl1hdik6q0qwl2/myreuselayout.zip?dl=0
Uses your code to remove buttons from layout. -
@mrjj
Now, it does not print any qDebugs() anymore... what the hell!? -
Close Creator, delete the build directory and start Creator again. Sometime strange things happen.
-
Ok, I did and now I have a mistake at a point in the code before the setting up of the layout.
I add a Tab of a tabWidget and there it crashes.,.. This was never a problem, this line works sonce weeks, I did not change anything there.
I deleted the build directory, opened creator again, ran qmake, rebuilt and that's it, no errors, it is just crashing -
@mrjj
Yep, this project works. So it has to be my bad. -
I say "unitialized memory somewhere".
-
I agree @Wieland , the big question is just Where :)
@Niagarer
If its a small project, can you zip and link it here ? -
Ahh about 1300 lines, unfortunately not small.
It works now... aaand yes, I think the deleting of the builddirectory worked.
What happened:
I deleted the build directory and then the program crashed at an other point before the setting up of the layout. I found a thing that I would not call a mistake, but it surely is in some way, because of my endless function calling, the function, where I set up the layouts got called twice. I don't really know, why this is a mistake, but after I fixed it, it worked and then the program was able to say that it is a nullpointer. So I changed it back to new QVBoxLayout and everything works.
The weird thing is, that I really don't know why it was a problem to setting up the layout as a nullpointer twice.
But, anyway, thanks for your help! -
Super :)
Im not sure what calling the function twice could be issue but it did do something it seems.