[How to delete a QList]
-
@Christian-Ehrlicher First of all thank you.
Well I used only list.clear() but I got an empty QList.
In fact here is the real thing;
I'm reading a csv file and storing all the lines in QList.
So I have about 54000 QLists since the file includes 54000 lines.
But my goal is to delete some lines that I won't be needing later in my program.
I thought after storing the lines in QLists, I would only keep the QLists that correponsd to these lines so I had to delete them completely.
After using list.clear() and using qDebug to see the output;
I get the lists i need and other lists that are empty.
-
@appdev said in [How to delete a QList]:
I get the lists i need and other lists that are empty.
Then don't use clear but only remove the indexes you don't need or copy all needed into a new container and clear the old one.
-
@appdev said in [How to delete a QList]:
I think the remove functions, only remove the item in the QList and not the QList itself.
?
You said you've a QList<QList<Item>> - so why should removeAt() on the outer container remove a single Item?
-
yes, I mean I have many QLists, about 54000.
QList [ value_1; value_2; value_3; value_4; value_5]
QList [ value_1; value_2; value_3; value_4; value_5]
QList [ value_1; value_2; value_3; value_4; value_5]
QList [ value_1; value_2; value_3; value_4; value_5]
QList [ value_1; value_2; value_3; value_4; value_5]...... until 54000.I want to delete all the QLists which values at the third position or index are different from 05 and I want to keep only those which values at that position are equal to 5.
At the end I sohuld only have 24 QLists. -
And what's the problem - I already told you how to remove an element of a QList:
Iterate over your list (from the back) and remove every element you don't want
or
Iterator over your list and copy all elements you want to a new list. -
Okay Now I see, it does make sense but for QList which includes another QList.
Earlier I said that I have QList<QList<Item>> but that's not actually the case.
I have a QList <QString>.
So when I use the clear function, it deletes all of the QString elements.
When I use the QDebug to see if the list is gone or not, As an output I get an empty list.
But logically if I delete the list and use Qdebug(), I'm not supposed to see anything in the output of the application, not even an empty list.
I think there is a difference between deleting a list and deleting its items right ?
-
@appdev said in [How to delete a QList]:
I have a QList <QString>.
So I have about 54000 QLists since the file includes 54000 lines.
What now?
-
@appdev said in [How to delete a QList]:
But logically if I delete the list and use Qdebug(), I'm not supposed to see anything in the output of the application, not even an empty list.
Not true.
-
If you have a
QList
variable, you will always have a list, which may be empty or have items. But you still have aQList
. -
If instead you go for a
QList *
variable, which younew
, you can thendelete theQList
when you're done with it, and you won't have an empty list, you will have no list. (You will still have aQList *
variable, but it won't point to an (allocated)QList
.)
This is not particular to
QList
, it applies to C++ generally.Note, btw, that this is not advocating using a
QList *
over aQList
. AQList *
is just 4/8 bytes (plus the allocation overhead when younew
it), but an emptyQList
may be not much bigger than that anyway, so why bother with a pointer, allocating etc. etc. unless you have a specific need for that. -
-
Hi,
In addition to the good points made by my fellow, deleting a pointer to a QList does not delete the content of the list if they are also pointers.