Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] Linking QLineEdit and QLabel
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 :)
- A Noob.
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
Qnoobish last edited by
If you already have your GUI you could try to use the function
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:
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.
No problems guys, I got it to work anyways! This is the code I ended up using:
addfuel7 = ui->lineedit_addfuel_7->text().toDouble();
filledfuel = addfuel1 + addfuel2 + addfuel3 + addfuel4 + addfuel5 + addfuel6 + addfuel7;
prevfuel = ui->lineedit_prevfuel->text().toDouble();
totalfuel = filledfuel + prevfuel;
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!
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 :)
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.