Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QVector<QString> convert to double

    General and Desktop
    4
    6
    3719
    Loading More Posts
    • 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
      mr__710 last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • M
          mcosta last edited by

          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 Reply Quote 0
          • M
            mr__710 last edited by

            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 Reply Quote 0
            • TheBadger
              TheBadger last edited by TheBadger

              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 Reply Quote 0
              • M
                mr__710 last edited by

                I got it, thank you!

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post