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. Converting QList<QString> to QList<float>?
Forum Updated to NodeBB v4.3 + New Features

Converting QList<QString> to QList<float>?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.4k 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.
  • M Offline
    M Offline
    MScottM
    wrote on last edited by A Former User
    #1

    Hi all,

    In my code I call a function that reads a text file, then uses a RegEx to parse it and then it returns a QStringList of comma separated values that I'm searching for (example): "13:53:33,479, 323.05" - the first is a timestamp, the next is the ID of the value within the timestamp (i.e. at time 13:53:33 there were 479 values recorded) and the last is the specific value I'm looking for. This part is working great (though the regex seems to be slow!).

    I want to use the time/value pairs to graph the data in a QCustomPlot and I've been struggling to extract the value out and convert it to a list of floats for the graph. Using an answer from this forum, I have been able to separate the QStringList and create a QList with the three items:

    void MainWindow::graphSelected(QStringList _graphingData) {
        graphingData = _graphingData;
        QStringList a{graphingData};
            QList<QStringList> list;
            foreach (const QString &s, a) {
            list.append(s.split(","));
           }
            qDebug() << data;
        }
    

    which gives me this list (qDebug() << data): ("14:09:17", "843", " 396.824"), ("14:09:18", "342", " 396.824")...

    For the life of me, I can't seem to figure out how to then iterate through and pull out the third item ("396.824"), convert it from a list of strings to a list of floats.

    Any tips?

    Thanks and best regards!

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      Hi @MScottM,

      Converting to a list of floats should be pretty easy, something like:

      void graphSelected(const QStringList &_graphingData) {
          QList<QStringList> list;
          QList<float> floats;
          foreach (const QString &s, _graphingData) {
              const QStringList parts = s.split(",");
              list.append(parts);
              Q_ASSERT(parts.size() >= 3);
              floats.append(parts.at(2).toFloat());
          }
          qDebug() << list;
          qDebug() << floats;
      }
      

      This gives, for example:

      (("14:09:17", "843", " 396.824"), ("14:09:18", "342", " 396.824"))
      (396.824, 396.824)
      

      Where the first output line is a list of list of strings, whereas the second line is a list of floats with matching top-level indexes.

      Note also, I've added an a Q_ASSERT to say that we expect there will always be at least three items per record. This is fine for dev, but if this is not guaranteed, then drop the Q_ASSERT and do something like this instead:

      floats.append(parts.size() < 3 ? NAN : parts.at(2).toFloat());
      

      That will ensure that the two lists still maintain the same indexes when values are missing. Of course, you can detect / handle missing values lots of other ways too.

      Also, depending on your case, you might want to define a class or struct to hold the tuple (the three values on each row), and use a QList<MyRowType> instead of both QList<QStringList> and QList<float> together. But that depends on what you want to do once you've parsed the data.

      PS I see no need for regex anywhere here... if you explain what you're using it for, then we might be able to help there too.

      Cheers.

      1 Reply Last reply
      5
      • M Offline
        M Offline
        MScottM
        wrote on last edited by
        #3

        Yes! thanks @Paul-Colby - It works - as usual the code was simple, I just didn't have the experience to figure it out.

        1 Reply Last reply
        1

        • Login

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