How to delete a row of a QGridLayout
-
@maxime0601 said in How to delete a row of a QGridLayout:
I edited my code but with i < 4 the program crashs
i < 4only 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.
-
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
0tothis->rowCount() - 1? So yourrowexceeds 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
deletean item (which is still owned by the grid layout), shouldn't you do some kind of:delete this->takeAt(?) -
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.
Try to remove the widget from your layout (instead of hiding it).
-
@maxime0601
I already said/suggestedint row = this->rowCount(); ... this->itemAtPosition(row,i)->widget()->hide();Don't rows count from
0tothis->rowCount() - 1? So yourrowexceeds 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
deletean item (which is still owned by the grid layout), shouldn't you do some kind of:delete this->takeAt(?) -
@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...
-
-
@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
-
@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 - 1and 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.
-
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.... -
@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