Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set default values to QDoubleSpinBox

Tags:

c++

qt

I am doing ballistic calculations for a projectile. so in that I have a check box and based on check box I have to take the inputs i.e if check box is enabled then read values from double-spinbox and do ballistics calculations

else go by the default values of the double spin box for that I have written this code but I end up the error in setValue() so for my requirement wt method should I take.

if(ui->checkBox->isChecked())
{
    //if it is checked then take the values given on UI

    altitude= ui-doubleSpinBox_1>text();
    b_pressure= ui-doubleSpinBox_2>text();
    r_humidity= ui-doubleSpinBox_3>text();
    temp=  ui-doubleSpinBox_4>text();
}
else
{
    ///else take the default values

    altitude=ui-doubleSpinBox_1>setValue(0);
    b_pressure=ui-doubleSpinBox_2>setValue(29.53);
    r_humidity=ui-doubleSpinBox_3>setValue(0.78);
    temp=ui-doubleSpinBox_4>setValue(78);
}
like image 585
vinay Avatar asked Dec 07 '25 22:12

vinay


1 Answers

QDoubleSpinBox::setValue returns a (lack of) value of type void, for which there are no conversions to anything. You are trying to assign to (double?) variables, and the compiler is telling you this is impossible.

Instead, you should conditionally set the default values, then unconditionally read the values. This keeps the (disabled?) ui up to date.

if(!ui->checkBox->isChecked())
{
    // set the default values

    ui->doubleSpinBox_1->setValue(0);
    ui->doubleSpinBox_2->setValue(29.53);
    ui->doubleSpinBox_3->setValue(0.78);
    ui->doubleSpinBox_4->setValue(78);
}

altitude = ui->doubleSpinBox_1->value();
b_pressure = ui->doubleSpinBox_2->value();
r_humidity = ui->doubleSpinBox_3->value();
temp = ui->doubleSpinBox_4->value();

Alternately, you could conditionally set the variables with your defaults, and unconditionally set the UI from the variables

if(!ui->checkBox->isChecked())
{
    // set the default values

    altitude = 0;
    b_pressure = 29.53;
    r_humidity = 0.78;
    temp = 78;
}

ui->doubleSpinBox_1->setValue(altitude);
ui->doubleSpinBox_2->setValue(b_pressure);
ui->doubleSpinBox_3->setValue(r_humidity);
ui->doubleSpinBox_4->setValue(temp);
like image 196
Caleth Avatar answered Dec 10 '25 10:12

Caleth