I want to write a code that increases the value in the line edit when the checkbox is clicked.
-
@wtimm Connect a slot to https://doc.qt.io/qt-6/qabstractbutton.html#clicked signal ofrom your check box. In that slot get the value of the line edit using https://doc.qt.io/qt-6/qlineedit.html#text-prop. Convert that text to number using https://doc.qt.io/qt-6/qstring.html#toInt. Increment this number and set it again in your text edit using https://doc.qt.io/qt-6/qstring.html#number-1 and https://doc.qt.io/qt-6/qlineedit.html#text-prop
If your line edit only accepts numbers you should use https://doc.qt.io/qt-6/qspinbox.html instead.
-
I want it to increase from U12C5 to U12C6 but when I write the code it returns 1. Is it possible to do what I want in qt
@wtimm
Please paste code text not screenshots, so others can copy/paste etc.QString::number(+1)
does not add one to anything. And you cannot directly add 1 toU12C5
anyway, since it is not a number. Either convert to a number, add to it, and convert it back to whatever your string represents, or write some code to add toU12C5
in whatever way your addition is supposed to work. -
@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?!