Convert QStringList to QList<const QString>
-
My problem is that I have a model containing a lot of data and I want to work as memory-efficient as possible.
I want to save the data in my model in QVector<const QString>. I want to use const QStrings, because I don't want Qt to preallocate memory for every single QString. That is not necessary, because the whole model is Read-only.
The data comes from a file, and I use QString.split to devide it. Split returns a QStringList, which is a QList<QString>. I could unfortunately not figure out how to convert the QStringList<QString> to a QStringList<const QString>. Is there a possiblility to do that? Or is the only possibility to save the model data as QVector<QString> (without const), and then call resize and squeeze() for every single entry?
Unfortunatly I could not find any relevant information about this in the Qt Forums, this is why I ask that Question here.
-
hi, friend, welcome devnet.
i tred way of your said, it is unallowed convertQStringList
to convertQList<const QString> or QVector<const QString>
direct.
we know than const variable is unallowed to modify after initalization.
in my sight, i hasve to use theconst QVector<QString> strVe = strlt.toVector()
, like the following code:#include <QCoreApplication> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString str = "one,two,three,four"; QStringList strlt = str.split(QChar(',')); // QVector<const QString> strVe = strlt.toVector(); // not allow const QVector<QString> strVe = strlt.toVector(); qDebug() << strVe; strVe[0] = "five"; // unallowed to modify the data /** strVe.cbegin(); strVe.constBegin(); strVe.constData(); strVe.constFirst(); strVe.constLast(); */ return a.exec(); }
maybe some one has the better way.
-
Hi,
recently I learned something new from @Chris-Kawa .
Namly the existence ofQString::splitRef
it seems, this might be the solution for your situation :)
SplitRef does not return a copy of strings like normal split would do.
-
@randsfjorden "I want to use const QStrings, because I don't want Qt to preallocate memory for every single QString" - what do you mean? You need memory for each string you store in the vector. What does "preallocate" mean in this context?
-
Heyy
Thank you very much for replying that quickly. Unfortunately I wasn't notified about your answers, this is why I didn't see them before today.Experimented a little bit more with const QStrings and now I think that it was no good approach for that purpose, to safe memory in this particular case it was more efficient to call squeeze.
@joeQ I now used your approach. I used qDebug to check the capacities/lengths of the strings inside the QStringList i got from split, and found out that non of the strings in that list had a higher capacity than length. So it was unneccessary to call squeeze on these, but I used it for the resulting vector.
@jsulm Qt automatically allocates some more memory when a QString / QStringList / QVector is created, to avoid needing to copy the object if something gets added to it. If I for eksample initialize a QString with 5 letters, Qt would preallocate space in memory for 10, so that it doesn't need to copy it, when the user adds 5 more letters. This preallocation is what I wanted to avoid, because I know that I won't change the strings in the ReadOnly-model again.
@J-Hilk: I actually checked that possibility out as well :)
Thank you very much for helping me!
-
@randsfjorden From your description it wasn't clear what you mean. Now I understood what your question was about.