PDA

View Full Version : How to get data from inputs?



fearlay
17th May 2016, 20:28
Hello,

First of all, I want to warn you about my english - I'm not native.

I want to create a really simple app for Windows. It may look like this:
11942

First two inputs are QDoubleSpinBox types
Third input is QLineEdit type.
and finally, the Result is a QLabel

All I want to do is simply add/multiply NUMBER_1 by NUMBER_3. The Result has to be changing dynamically.

How to get data from those inputs? And then how to put this data into QLabel (Result)?

Any ideas?

Thanks.

Scope
17th May 2016, 21:23
QDoubleSpinBox::value()
QLineEdit::text()
QLabel::setText()

and read Qt Documentation.

fearlay
17th May 2016, 22:39
Thank you so much for the help!



double number_1 = ui->doubleSpinBox->value();
double number_3 = ui->lineEdit->text().toDouble();
double res = number_1*number_3;
QString r = QString::number(res);
ui->label_7->setText(r);


This works awesome! The only problem I have is that I want to display the result dynamically. I have already tried to do this by using Signals/Slots, but unfortunately I failed. Can someone give me a hint how to actually do this?

Thanks.

Scope
18th May 2016, 00:28
Look for signals:


QDoubleSpinBox::valueChanged(double d);
QLineEdit::textChanged(const QString & text);

ReshmaRatan
25th May 2016, 12:45
Hello,

As Scope is mentioning you can use the signals connected to different slots.
QDoubleSpinBox::valueChanged(double d);
QLineEdit::textChanged(const QString & text);


Here the tricky part for dynamically update is to have two bool variables, one for NUMBER_1 doublespinbox and the other for NUMBER_3 line edit. So in there respective slots you have to make them true and check for whether the other bool variable is true or not. If other variable is also true then proceed with the calculation and set the value to the Label. Important thing is we have to make both variables to false for next calculation.

//Sample Code looks like.
bool bIsNumber1 = false, bIsNumber3 = false;

//slot connected to DoubleSpinBox signal
void slotDoubleSpinNo1()
{
bIsNumber1 = true;
if(bIsNumber3)
{
//do calculation and set the text to label.
resultLable->setText();

//reset the bool variables
bIsNumber1 = false;
bIsNumber3 = false;
}
}

//Slot connected to LineEdit signal
void slotLineEditNo3()
{
bIsNumber3 = true;
if(bIsNumber1)
{
//do calculation and set the text to label.
resultLable->setText();

//reset the bool variables
bIsNumber1 = false;
bIsNumber3 = false;
}
}

Hope it helps....