Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert QStringList to QList<const QString>
Forum Updated to NodeBB v4.3 + New Features

Convert QStringList to QList<const QString>

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 7.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    randsfjorden
    wrote on 12 Feb 2017, 22:54 last edited by
    #1

    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.

    J J 2 Replies Last reply 13 Feb 2017, 03:15
    0
    • R randsfjorden
      12 Feb 2017, 22:54

      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.

      J Offline
      J Offline
      joeQ
      wrote on 13 Feb 2017, 03:15 last edited by
      #2

      @randsfjorden

      hi, friend, welcome devnet.
      i tred way of your said, it is unallowed convert QStringList to convert QList<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 the const 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.

      Just do it!

      1 Reply Last reply
      0
      • J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 13 Feb 2017, 05:32 last edited by
        #3

        Hi,
        recently I learned something new from @Chris-Kawa .
        Namly the existence of

        QString::splitRef
        

        it seems, this might be the solution for your situation :)

        SplitRef does not return a copy of strings like normal split would do.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • R randsfjorden
          12 Feb 2017, 22:54

          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.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 13 Feb 2017, 05:33 last edited by
          #4

          @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?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • R Offline
            R Offline
            randsfjorden
            wrote on 27 Feb 2017, 17:01 last edited by
            #5

            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!

            J 1 Reply Last reply 28 Feb 2017, 05:17
            0
            • R randsfjorden
              27 Feb 2017, 17:01

              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!

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 28 Feb 2017, 05:17 last edited by
              #6

              @randsfjorden From your description it wasn't clear what you mean. Now I understood what your question was about.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved