I want to write a code that increases the value in the line edit when the checkbox is clicked.
-
@wtimm
For that1111
, which is now a number, you could use something like:auto val = ui->lineEdit->text().toInt(); val += ui->spinBox->value(); ui->lineEdit->setText(QString::number(val));
However, there is something hokey about trying to use a
QSpinBox
, with its own value and a button, to increase a number in aQLineEdit
. In the example you now show, why not make Serial Number aQSpinBox
itself and dispense with the separate incrementer and pushbutton? -
ui->spinBox->setValue(08246);
The code throws an error when I enter such a value, so I thought it would be better to use QLineEdit.
@wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:
ui->spinBox->setValue(08246);
The code throws an error when I enter such a valueThe code throws an error? At runtime? Don't you mean the compiler throws an error on this?
08246
as a C++ literal is octal, because of the leading0
, and then8
is not a valid digit in base 8!If you want to retain a leading
0
, so it's not "really" "just" a number, that is harder (also in aQSpinBox
if you changed to use that, though still doable). As it is, this is why @J-Hilk's code shows how to achieve it. -
auto val = ui->lineEdit->text().toInt(); auto originalLength = ui->lineEdit->text().length(); val += ui->spinBox->value(); ui->lineEdit->setText(QString::number(val).rightJustified(originalLength, '0'));
-
@wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:
if i use originalLength function i got use of undeclared identifier 'originalLength' error
@J-Hilk said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:
auto originalLength = ui->lineEdit->text().length();
ui->lineEdit->setText(QString::number(val).rightJustified(originalLength, '0'));
It's not a "function". So how did you get "undeclared identifier"?
-
int v = settings.value(number(val),defaultValue).toInt();
I wrote such a code but it didn't work.
When I save the increased value, how can I make the increased value show as default when I open the project again?@wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:
When I save the increased value, how can I make the increased value show as default when I open the project again?
Save the increased (always the current) value when you close your project?!
-
int v = settings.value(number(val),defaultValue).toInt();
I wrote such a code but it didn't work.
When I save the increased value, how can I make the increased value show as default when I open the project again?@wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:
I wrote such a code but it didn't work.
Meaning what? Please don't just say "it didn't work" if you want people to understand enough to help. Your code shows reading back a value. How do we know what you saved where how? Did you look in the external INI file or Windows Registry to see what is there which has been saved?
When I save the increased value, how can I make the increased value show as default when I open the project again?
Read it back in somewhere at startup and set the widget's value from the previously saved.
-
@wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:
When I save the increased value, how can I make the increased value show as default when I open the project again?
Save the increased (always the current) value when you close your project?!
-
@wtimm Already told you to save it via
QSettings
, there really isn't anything else to say....@JonB we have defined the variable val in the above answers (the value in the line edit)
Can we do it if we define val inside the setValue function or should we do it using another function?
void Widget::on_grabDataButton_clicked()
{
QSettings settings;
settings.setValue("val",1);
settings.value("val").toInt();
} -
@JonB we have defined the variable val in the above answers (the value in the line edit)
Can we do it if we define val inside the setValue function or should we do it using another function?
void Widget::on_grabDataButton_clicked()
{
QSettings settings;
settings.setValue("val",1);
settings.value("val").toInt();
}@wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:
void Widget::on_grabDataButton_clicked()
{
QSettings settings;
settings.setValue("val",1);
settings.value("val").toInt();
}Dont know what this should do. Maybe you are using
QSettings
wrong.To set value
settings.setValue("val", 42);
To read the value:
int val = settings.value("val").toInt();
And do whatever you like with
val
(set it to your line edit after you start your program) -
@wtimm Already told you to save it via
QSettings
, there really isn't anything else to say....