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. How to pass String Value in decimal to QlineEdit?
Forum Updated to NodeBB v4.3 + New Features

How to pass String Value in decimal to QlineEdit?

Scheduled Pinned Locked Moved Unsolved General and Desktop
33 Posts 6 Posters 4.9k 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.
  • W Offline
    W Offline
    Wasee
    wrote on 26 Nov 2021, 13:30 last edited by
    #1

    Dear All;
    I am using QProcess to reading continuous value in string but now I want to convert this string to decimal value. After that I want to put this value in qlineedit.
    Thanks

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 26 Nov 2021, 14:57 last edited by
      #2

      @Wasee said in How to pass String Value in decimal to QlineEdit?:

      now I want to convert this string to decimal value.

      Then do it... https://doc.qt.io/qt-5/qstring.html

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JoeCFD
        wrote on 26 Nov 2021, 15:22 last edited by
        #3
        std::string continuous_value =***;
        QString str( QString::fromStdString( continuous_value ) );
        bool ok{ false };
        double or float value = str.toDouble( &ok );
        if ( true == ok ) {
            line edit->setText( QString::number( value ) );
        /* if format is needed */
            line edit->setText( QString( "%1" ).arg( value, some format ) );
        }
        
        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wasee
          wrote on 27 Nov 2021, 12:02 last edited by
          #4

          @Christian-Ehrlicher @JoeCFD
          I am reading like this:
          /@
          QProcess Temp_reading;
          Temp_reading.start("sudo devmem2 0x80000000");
          Temp_reading.waitForFinished(-1); // will wait forever until finished
          QString Temp_read = Temp_reading.readAllStandardOutput();

          ui->lineedit->setText(Temp_read);

          //Its giving me value 0x1 in hex format but I need to convert in decimal value and pass to lineedit//

          @/

          J C 2 Replies Last reply 27 Nov 2021, 13:36
          0
          • W Wasee
            27 Nov 2021, 12:02

            @Christian-Ehrlicher @JoeCFD
            I am reading like this:
            /@
            QProcess Temp_reading;
            Temp_reading.start("sudo devmem2 0x80000000");
            Temp_reading.waitForFinished(-1); // will wait forever until finished
            QString Temp_read = Temp_reading.readAllStandardOutput();

            ui->lineedit->setText(Temp_read);

            //Its giving me value 0x1 in hex format but I need to convert in decimal value and pass to lineedit//

            @/

            J Offline
            J Offline
            JonB
            wrote on 27 Nov 2021, 13:36 last edited by
            #5

            @Wasee

            QString str = "0x1";
            bool ok;
            int num = str.toInt(&ok, 0);
            
            1 Reply Last reply
            0
            • W Wasee
              27 Nov 2021, 12:02

              @Christian-Ehrlicher @JoeCFD
              I am reading like this:
              /@
              QProcess Temp_reading;
              Temp_reading.start("sudo devmem2 0x80000000");
              Temp_reading.waitForFinished(-1); // will wait forever until finished
              QString Temp_read = Temp_reading.readAllStandardOutput();

              ui->lineedit->setText(Temp_read);

              //Its giving me value 0x1 in hex format but I need to convert in decimal value and pass to lineedit//

              @/

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 27 Nov 2021, 15:23 last edited by
              #6

              @Wasee said in How to pass String Value in decimal to QlineEdit?:

              but I need to convert in decimal value and pass to lineedit//

              Then convert it from hex string to a double value and then back to a QString as it's documented in the documentation (link in my first post)

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • W Offline
                W Offline
                Wasee
                wrote on 29 Nov 2021, 04:36 last edited by
                #7

                @Christian-Ehrlicher @JoeCFD @JonB ;
                Thanks to your reply! I follow the following code but get again conversion errors.
                "/@
                QProcess Temp_reading;
                std:: string ct_value=Temp_reading.start("sudo devmem2 0x80000000");
                QString str (QString::fromStdString(ct_value));
                bool okk{false};
                double or float value = str.toDuble(&okk);
                if(true == &okk){
                ui->linedit->setText(QString::number(value));
                }

                Thanks in advance

                J 1 Reply Last reply 29 Nov 2021, 06:28
                0
                • W Wasee
                  29 Nov 2021, 04:36

                  @Christian-Ehrlicher @JoeCFD @JonB ;
                  Thanks to your reply! I follow the following code but get again conversion errors.
                  "/@
                  QProcess Temp_reading;
                  std:: string ct_value=Temp_reading.start("sudo devmem2 0x80000000");
                  QString str (QString::fromStdString(ct_value));
                  bool okk{false};
                  double or float value = str.toDuble(&okk);
                  if(true == &okk){
                  ui->linedit->setText(QString::number(value));
                  }

                  Thanks in advance

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 29 Nov 2021, 06:28 last edited by
                  #8

                  @Wasee said in How to pass String Value in decimal to QlineEdit?:

                  "sudo devmem2 0x80000000"

                  Do you realise that your string contains not only a number but allso non-number string ("sudo devmem2 ")?
                  That part of course can't be converted to a number.
                  You need first to extract the hex-number part and only convert that part to a number.
                  You can use https://doc.qt.io/qt-5/qstring.html#split to split your string using "0x" as separator.

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

                  J 1 Reply Last reply 29 Nov 2021, 09:53
                  0
                  • J jsulm
                    29 Nov 2021, 06:28

                    @Wasee said in How to pass String Value in decimal to QlineEdit?:

                    "sudo devmem2 0x80000000"

                    Do you realise that your string contains not only a number but allso non-number string ("sudo devmem2 ")?
                    That part of course can't be converted to a number.
                    You need first to extract the hex-number part and only convert that part to a number.
                    You can use https://doc.qt.io/qt-5/qstring.html#split to split your string using "0x" as separator.

                    J Offline
                    J Offline
                    JonB
                    wrote on 29 Nov 2021, 09:53 last edited by JonB
                    #9

                    @jsulm
                    I think you need to clean your reading glasses and try again... ;-)

                    The OP is using that string as argument to QProcess::start(), not as the string to parse!

                    @Wasee

                    QProcess Temp_reading;
                    std:: string ct_value=Temp_reading.start("sudo devmem2 0x80000000");
                    QString str (QString::fromStdString(ct_value));
                    

                    void QProcess::start() does not return any value, so this won't compile, will it....

                    Can you just slow down a bit. What are you actually trying to do? I can only guess you have in mind:

                    Temp_reading.start("sudo devmem2 0x80000000");
                    Temp_reading.waitForFinished();
                    QString output(Temp_reading.readAllStandardOutput());
                    bool ok;
                    int num = output.toInt(&ok, 0);
                    

                    Actually I see you had this earlier. All you needed to do was do the QString::toInt() on what you got back followed by QString::number() to put it into the line edit. And if that is delivering the right answer have a look at the output from the command which you are trying to parse.

                    P.S.
                    You originally said the number you need to parse is (like) 0x1. I showed you the above code with output.toInt(&ok, 0); is how to do that. If you are using QString::toDouble() as you show that will not work, as it cannot parse a leading 0x.

                    W 1 Reply Last reply 1 Dec 2021, 08:13
                    2
                    • W Offline
                      W Offline
                      Wasee
                      wrote on 1 Dec 2021, 07:51 last edited by
                      #10

                      @jsulm Hi;
                      I followed following code to split the string
                      /@
                      QString band1_2="sudo devmem2 0x80000000 w 0x";
                      QString str = QStringLiteral("sudo devmem2 0x80000000 w 0x");
                      QStringList list1 = str.split(QLatin1Char('.'));
                      qDebug()<<"Split:"<<list1;
                      @/
                      Its giving me value like "11". But it is not passing to lineedit why? Even I code to convert into integer below.
                      QString list;
                      bool ok;
                      int d;
                      d = Qstring(list).toInt(&ok);
                      ui->lineedit->setText(d);

                      Thanks in advance

                      J 1 Reply Last reply 1 Dec 2021, 07:58
                      0
                      • W Wasee
                        1 Dec 2021, 07:51

                        @jsulm Hi;
                        I followed following code to split the string
                        /@
                        QString band1_2="sudo devmem2 0x80000000 w 0x";
                        QString str = QStringLiteral("sudo devmem2 0x80000000 w 0x");
                        QStringList list1 = str.split(QLatin1Char('.'));
                        qDebug()<<"Split:"<<list1;
                        @/
                        Its giving me value like "11". But it is not passing to lineedit why? Even I code to convert into integer below.
                        QString list;
                        bool ok;
                        int d;
                        d = Qstring(list).toInt(&ok);
                        ui->lineedit->setText(d);

                        Thanks in advance

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 1 Dec 2021, 07:58 last edited by
                        #11

                        @Wasee said in How to pass String Value in decimal to QlineEdit?:

                        QString list;
                        bool ok;
                        int d;
                        d = Qstring(list).toInt(&ok);

                        This does not make sense at all: you create a string from an empty string (list) and then try to convert it to int.

                        Please read once more what I wrote. I never suggested to split using '."

                        But actually you should read what @JonB suggested, as my answer was based on my misunderstanding.

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

                        1 Reply Last reply
                        1
                        • J JonB
                          29 Nov 2021, 09:53

                          @jsulm
                          I think you need to clean your reading glasses and try again... ;-)

                          The OP is using that string as argument to QProcess::start(), not as the string to parse!

                          @Wasee

                          QProcess Temp_reading;
                          std:: string ct_value=Temp_reading.start("sudo devmem2 0x80000000");
                          QString str (QString::fromStdString(ct_value));
                          

                          void QProcess::start() does not return any value, so this won't compile, will it....

                          Can you just slow down a bit. What are you actually trying to do? I can only guess you have in mind:

                          Temp_reading.start("sudo devmem2 0x80000000");
                          Temp_reading.waitForFinished();
                          QString output(Temp_reading.readAllStandardOutput());
                          bool ok;
                          int num = output.toInt(&ok, 0);
                          

                          Actually I see you had this earlier. All you needed to do was do the QString::toInt() on what you got back followed by QString::number() to put it into the line edit. And if that is delivering the right answer have a look at the output from the command which you are trying to parse.

                          P.S.
                          You originally said the number you need to parse is (like) 0x1. I showed you the above code with output.toInt(&ok, 0); is how to do that. If you are using QString::toDouble() as you show that will not work, as it cannot parse a leading 0x.

                          W Offline
                          W Offline
                          Wasee
                          wrote on 1 Dec 2021, 08:13 last edited by
                          #12

                          @JonB Hi;
                          How I can resolve problem i didn't understand.Please write piece of code.
                          thanks

                          J 1 Reply Last reply 1 Dec 2021, 08:13
                          0
                          • W Wasee
                            1 Dec 2021, 08:13

                            @JonB Hi;
                            How I can resolve problem i didn't understand.Please write piece of code.
                            thanks

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 1 Dec 2021, 08:13 last edited by
                            #13

                            @Wasee said in How to pass String Value in decimal to QlineEdit?:

                            .Please write piece of code

                            He already did.
                            What is not clear?

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

                            1 Reply Last reply
                            1
                            • W Offline
                              W Offline
                              Wasee
                              wrote on 1 Dec 2021, 08:23 last edited by
                              #14

                              @jsulm ;
                              Thanks!
                              But I am getting error of invalid conversion from int to const string for lineedit.

                              ui->lineedit->setText(num);

                              J 1 Reply Last reply 1 Dec 2021, 08:36
                              0
                              • W Wasee
                                1 Dec 2021, 08:23

                                @jsulm ;
                                Thanks!
                                But I am getting error of invalid conversion from int to const string for lineedit.

                                ui->lineedit->setText(num);

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 1 Dec 2021, 08:36 last edited by jsulm 12 Jan 2021, 08:36
                                #15

                                @Wasee Do you understand that setText expects a string, not int?

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

                                1 Reply Last reply
                                3
                                • W Offline
                                  W Offline
                                  Wasee
                                  wrote on 3 Dec 2021, 06:26 last edited by
                                  #16

                                  Hi; everyone;
                                  I am unable to fix my problem. Please provide exact information to do that.

                                  J 1 Reply Last reply 3 Dec 2021, 06:31
                                  0
                                  • W Wasee
                                    3 Dec 2021, 06:26

                                    Hi; everyone;
                                    I am unable to fix my problem. Please provide exact information to do that.

                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 3 Dec 2021, 06:31 last edited by
                                    #17

                                    @Wasee said in How to pass String Value in decimal to QlineEdit?:

                                    Please provide exact information to do that

                                    I will do once more your work, but last time now. I will not spend more of my time if you refuse to learn basics.
                                    Go to https://doc.qt.io/qt-5/qlineedit.html#text-prop and check what the type is.
                                    Type is QString.
                                    C++ most basic knowledge: QString != int.
                                    So, you can't pass an int as parameter to QLineEdit::setText, because setText() expects a QString.
                                    I really fail to understand what is not clear about it?!
                                    Maybe you should read a C++ book?

                                    Temp_reading.start("sudo devmem2 0x80000000");
                                    Temp_reading.waitForFinished();
                                    QString output(Temp_reading.readAllStandardOutput());
                                    ui->lineedit->setText(output);
                                    

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

                                    1 Reply Last reply
                                    1
                                    • W Offline
                                      W Offline
                                      Wasee
                                      wrote on 3 Dec 2021, 06:53 last edited by
                                      #18

                                      @jsulm Hi;
                                      Its giving me Values continuous but I need to print these values after 0x in integer form in line-edit.
                                      (0xb6f44000):0x11EACAC
                                      (0xb6efe00):0x11EACAC
                                      (0xb6fd4000):0x11EACAC
                                      (0xb6fde000):0x11EACAC

                                      J 1 Reply Last reply 3 Dec 2021, 07:04
                                      0
                                      • W Wasee
                                        3 Dec 2021, 06:53

                                        @jsulm Hi;
                                        Its giving me Values continuous but I need to print these values after 0x in integer form in line-edit.
                                        (0xb6f44000):0x11EACAC
                                        (0xb6efe00):0x11EACAC
                                        (0xb6fd4000):0x11EACAC
                                        (0xb6fde000):0x11EACAC

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 3 Dec 2021, 07:04 last edited by
                                        #19

                                        @Wasee What's wrong with https://doc.qt.io/qt-5/qstring.html#number-1?

                                        d = Qstring(list).toInt(&ok, 16); // Since you handle hex numbers you need to tell toInt that it is a hex number - so pass 16 as base
                                        ui->lineedit->setText(QString::number(d));
                                        

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

                                        1 Reply Last reply
                                        1
                                        • W Offline
                                          W Offline
                                          Wasee
                                          wrote on 3 Dec 2021, 07:16 last edited by
                                          #20

                                          @jsulm Hi;
                                          Its giving me value 0 in my lineedit.

                                          J 1 Reply Last reply 3 Dec 2021, 07:20
                                          0

                                          1/33

                                          26 Nov 2021, 13:30

                                          • Login

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