vinay
vinay

Reputation: 37

How can I set default values to QDoubleSpinBox

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);
}

Upvotes: 3

Views: 5708

Answers (1)

Caleth
Caleth

Reputation: 62531

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);

Upvotes: 3

Related Questions