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. QVector<QString> convert to double

QVector<QString> convert to double

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 4.2k Views
  • 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
    mr__710
    wrote on last edited by
    #1

    Hello everybody,

    i have a vector filled with Strings and i want to convert them into double numbers to use them as data points in a graph. The Vector is filled with Strings, because it gets the values from a table.

    My current code looks like this:

    QVector<double>  x(num_rows);
    QVector<QString> y(num_rows);
    for (int i=0; i<num_rows; ++i)
    {
      x[i] = 1.5*i;
      y[i] = ui->tableWidget->item(i,2)->text().toDouble();
    }
    

    The result is:
    QVector("%", "\u0000", "\u0000", "\u0000", "\u0000", "\u0000", "%")
    instead of:
    QVector(37, 36,8, 36,9, 36,7, 36,9, 36,8, 37)

    Thanks for your help.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Your y vector looks like that because you made it like a QVector<QString>. Since you already convert your QTableWidget content to double, why not make it a QVector<double> like x ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        y[i] = ui->tableWidget->item(i,2)->text().toDouble();
        

        you're converting a text into number and then assign to a QString!!!

        Have you tried with

        QVector<double> x(num_rows);
        QVector<double> y(num_rows); // <-- Now is a Vector of double
        for (int i=0; i<num_rows; ++i)
        {
          x[i] = 1.5*i;
          y[i] = ui->tableWidget->item(i,2)->text().toDouble();
        }
        

        ??

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mr__710
          wrote on last edited by
          #4

          it works. Thank you!

          One more Question. The values for x[i] are in sql datetime format (yyyy-mm-dd hh:mm:ss). And in my Vector they are converted to Strings. How can i use these data in the right way? My aim is to plot them on xAxis, using QCustomPlot.

          QVector<QString>  x(num_rows);
          QVector<double> y(num_rows);
          for (int i=0; i<num_rows; ++i)
          {
            x[i] = ui->tableWidget->item(i,1)->text();
            y[i] = ui->tableWidget->item(i,2)->text().toDouble();
          }
          

          response:QVector("2015-11-17 18:50:00",......)

          1 Reply Last reply
          0
          • TheBadgerT Offline
            TheBadgerT Offline
            TheBadger
            wrote on last edited by TheBadger
            #5

            Most Qt SQL drivers return the value as QVariant, from there you can normally use the QVariant::toDate() or QVariant::toDateTime() functions.

            Otherwise you need to use the static QDateTime::fromString() function, just set the format correctly.

            QDateTime date = QDateTime::fromString(x[i], "yyyy-MM-dd hh:mm:ss");
            

            PS: the above format is just an example, you need to update based on your format that you have but it looks like it should work based on your response:

            response:QVector("2015-11-17 18:50:00",......)


            Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mr__710
              wrote on last edited by
              #6

              I got it, thank you!

              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