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. [Beginner]Calculate two values from lineEdit
Qt 6.11 is out! See what's new in the release blog

[Beginner]Calculate two values from lineEdit

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 6.0k Views 1 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.
  • E Offline
    E Offline
    eager2learn
    wrote on last edited by
    #1

    Hi, I am new to programming and trying to learn new things every day.
    I am trying to build a program in Qt to calculate some things. I have made severel input boxes (lineEdit) and those values I want to calculate and display in new labels.

    This is the code I have now. I think the second line is wrong. I want to do the input (a number) of lineEdit * the input of lineEdit_2. I think I need to transform the boxes from strings to numbers or something. I hope you can help me further. Thanks.

    @
    int newvalue;
    newvalue = lineEdit.text * lineEdit_2.text;
    ui->label_2->setText(newvalue);
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Anticross
      wrote on last edited by
      #2

      First you need use text() not text :)
      @lineEdit.text()@
      that gives you a QString value which you need to convert to int using toInt(). Then you do your calculations with int values end call setText(QString("%1").arg(newvalue));

      1 Reply Last reply
      0
      • E Offline
        E Offline
        eager2learn
        wrote on last edited by
        #3

        [quote author="Anticross" date="1326276325"]First you need use text() not text :) @lineEdit.text()@ that gives you a QString value which you need to convert to int using toInt(). Then you do your calculations with int values end call setText(QString("%1").arg(newvalue));[/quote]
        Thanks. I have added the () and did some toInt() conversion.
        Now I have:
        @
        int number1;
        number1 = ui->lineEdit->text().toInt();
        int number2;
        number2 = ui->lineEdit_2->text().toInt();
        int newvalue;
        newvalue = number1 * number2;
        ui->label_7->setText(""+newvalue);
        @
        I guess this is a bit better. But still not good. I do not understand the QString("%1").arg stuff yet so I tried it this way which sounded logically after reading tutorials. But it is not, I guess;)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          If you change the last line to something like this, it should work:
          @
          ui->label_7->setText(QString::number(newValue));
          @
          or
          @
          ui->label_7->setText(QString("%1").arg(newValue));
          @

          1 Reply Last reply
          0
          • B Offline
            B Offline
            broadpeak
            wrote on last edited by
            #5

            Because you have very basic knowledge about Qt, read this book:
            http://www.qtrac.eu/C++-GUI-Programming-with-Qt-4-1st-ed.zip
            This is an excellent "how-to-start-with-qt" book.
            This book is free, and this is THE official Qt book.
            (The 2nd edition of this book is not free, you can buy it.)

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              Yasin
              wrote on last edited by
              #6

              "This is the right link.":http://www.qtrac.eu/C++-GUI-Programming-with-Qt-4-1st-ed.zip

              Yasin Gorgij

              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