Unsolved How to delete a row of a QGridLayout
-
int row = this->rowCount(); ... while ((child = this->takeAt(row)) != nullptr) {
Don't rows (for
takeAt(row)
) count from 0 torowCount() - 1
? So your code will do nothing (docs: "If there is no such item, the function must do nothing and return 0."), and never enter thewhile
loop?As for the commented-out attempt:
// for (int i(0); i > 4 ; ++i){ // this->itemAtPosition(row,i)->widget()->hide(); // delete this->itemAtPosition(row,i); // }
won't do what you intend because youThat apart,delete
ati
and then incrementi
, not taking into account that you have done a deletion which squeezes other items down (i.e. it deletes every other item)?i
starts at 0 and your loop while condition isi > 4
, so it will never enter the loop anyway.In both these cases you would find out what is/is not happening if you put some kind of trace/debug statements into your code.
-
@jonb
For the part with the takeAt(row), I think that I haven't really understand the method. However, it works but it delete more than I hoped.For the other part, the i > 4 is just a mistake when I edited my code but with i < 4 the program crashs. If I understand well your explain I just have to delete the first element and after it will delete others under it?
-
@maxime0601 said in How to delete a row of a QGridLayout:
I edited my code but with i < 4 the program crashs
i < 4
only works if you have 4 items in your Grid, otherwise there is no column 3.You have to replace the 4 with the number of your current cols.
-
int row = this->rowCount(); int col = this->columnCount(); if (row > 6) { for (int i(0); i < col ; ++i){ this->itemAtPosition(row,i)->widget()->hide(); delete this->itemAtPosition(row,i); } } }
It also crashs like this.
-
@maxime0601
I already said/suggestedint row = this->rowCount(); ... this->itemAtPosition(row,i)->widget()->hide();
Don't rows count from
0
tothis->rowCount() - 1
? So yourrow
exceeds the number of rows, hence crashes? Test whatthis->itemAtPosition(row, 0)
returns, or crashes?Then when you have this right, I don't think you should directly
delete
an item (which is still owned by the grid layout), shouldn't you do some kind of:delete this->takeAt(?)
-
Try to remove the widget from your layout (instead of hiding it).
-
-
@maxime0601 said in How to delete a row of a QGridLayout:
It continues to crash
then please use the debugger to see what exactly happens and where...
-
-
@jsulm I have a signal of segmentation fault. It confirms that I try to use an outside element of my grid
-
@maxime0601 said in How to delete a row of a QGridLayout:
I try to use an outside element of my grid
Well, then fix that. And I know that it is crashing and my suggestion to use debugger is still valid: you will see where exactly it is crashing and what the values of variables at that time.
Did you read what @JonB wrote? -
-
@jonb I'm sorry if I don't understand what you mean but when I tried itemAtPosition(row,0) it crashes.
Which is why I said your program crashes all the time! You are not understanding that rows are numbered starting from 0 and going to
rowCount() - 1
, not torowCount()
. That's how most arrays etc. are numbered in C/C++. I don't know how to explain any better, you really need to understand this fundamental, rather than banging stuff in without understanding:Oh I tried with
row - 1
and it seems worksYou really need to understand why that it is, rather than "seems to work".
-
int row = this->rowCount(); int col = this->columnCount(); if (row > 6) { for (int i(0); i < col ; ++i) { this->itemAtPosition(row - 1,i)->widget()->hide(); delete this->takeAt(row - 1); } }
So, when I used this code, the last row disappeared as I want but the disposition of the others elements in the grid changes.
@JonB yes I knew this for vector but I totally forgot it. I'm sorry for that.
-
@maxime0601
I'm sorry but it's more complex than you are expecting. You really need to read https://stackoverflow.com/questions/5395266/removing-widgets-from-qgridlayout. I know it looks a lot more involved than what you are trying, but there you are.... -
@jonb Thanks a lot, I've searched but I don't found similar topic. I think that I can succeed with this.
Thanks a lot everyone for your help