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. [SOLVED] Linking QLineEdit and QLabel

[SOLVED] Linking QLineEdit and QLabel

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.7k 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.
  • S Offline
    S Offline
    SimenZhor
    wrote on last edited by
    #1

    Hi all.

    I am very new to all sort of programming (started learning C++ a week ago), so please bear with me.

    I have some simple questions about Qt that I have been trying to read up on the last two or three days, but have been unable to figure out so far. This is mainly due to lack of competence in C++, but I am doing my best to learn more in that department.

    So, the reason I ask help now, is because I really don't have use for advanced knowledge in Qt yet, seeing as I still learn C++. But I have a fully functional C++ program that I need to make a simple GUI for as quick as possible with as little as possible trouble.

    The program will consist of 7 QLineEdit (where the user shall input numbers from 0 to 99) that are to be added together and the result shown in a QLabel.

    The sum from the previous calculation are then to be added with another user input before it is substracted with a third user input (both QLineEdit).

    There are a couple more calculations that are supposed to happen, but if I can understand what I have mentioned so far I will be able to do the rest myself.

    So, the problems I am having is collecting the input from QLineEdit and do calculations with it. I don't fully understand if it is possible to do this while sticking with ints and doubles (as I could to in the win32 program) or if I need to use a QString. Either way, I haven't been able to calculate with the QString so far, and I haven't been able to print a double into a label using setText();, so I fear I will have to do some converting in between here that I can't seem to figure out.

    So far I have the design of the GUI completed, but cant get the elements to interact with each other. Would appreciate examples if anyone are willing to help me out :)

    Best regards

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

      Hi and welcome to devnet,

      you might not be using the best classes, have a look at the "QSpinBox":http://qt-project.org/doc/qt-4.8/qspinbox.html for number input.

      Also look at "QString::number":http://qt-project.org/doc/qt-4.8/qstring.html#number

      Hope it helps

      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
      • Q Offline
        Q Offline
        Qnoobish
        wrote on last edited by
        #3

        If you already have your GUI you could try to use the function

        @QString::toInt()@

        to get an integer and then do some calculations on the number. You could try using a similar function to convert it to string, like:

        @QString::number(yourInt)@

        So I think by doing something similar you could work on your other types of variables like doubles and such.

        here are some forum links to this topics with some examples hopefully they would help.

        "int to QString":http://stackoverflow.com/questions/3211771/how-to-convert-int-to-qstring

        "QString to int":http://developer.nokia.com/Community/Discussion/showthread.php/202451-how-to-convet-QString-into-int

        In short, software is eating the world.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimenZhor
          wrote on last edited by
          #4

          No problems guys, I got it to work anyways! This is the code I ended up using:

          @
          void MainWindow::on_lineedit_7_textEdited()
          {
          addfuel7 = ui->lineedit_addfuel_7->text().toDouble();
          filledfuel = addfuel1 + addfuel2 + addfuel3 + addfuel4 + addfuel5 + addfuel6 + addfuel7;
          ui->label_filledfuel->setText(QString::number(filledfuel));
          }

          //Fuel Functions

          void MainWindow::on_lineedit_prevfuel_textEdited()
          {
          prevfuel = ui->lineedit_prevfuel->text().toDouble();
          totalfuel = filledfuel + prevfuel;
          ui->label_totalfuel->setText(QString::number(totalfuel));
          }
          @

          I know it is not the smoothest bit of code on the internet, but since it is a very small program with just 12 inputs it worked out fine this way :) Thanks for the help anyways though!

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

            You're welcome !

            I would however recommend that you take a look again at the QSpinBox/QDoubleSpinBox. If your users write something that is not a number, you won't have the result wanted.

            Don't forget to update the thread title prepending [solved] so other forum users may know a solution has been found :)

            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
            • S Offline
              S Offline
              SimenZhor
              wrote on last edited by
              #6

              I originally used a spinbox, but figured this worked smoother. The user shouldn't need to input anything else than numbers anyways. I'll add an if statement where I display an error message if anything else than numbers 0-60 are plotted in (as you might have noticed the program is designed to calculate fuel-usage through a month), 60l is the size of the fuel tank for the car type.

              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