PDA

View Full Version : Edit digit at cursor position in a QlineEdit



Egeris
3rd August 2018, 16:53
I desire to make a smart-edit feature to a program, so that it's possible to edit a numeric value on a QlineEdit by pressing up- or down-arrow-key.

See attachment to get the idea:
12920

What I have so far:
- QLineEdit in a mainwindow
- An eventFilter which detects when I press the up-arrow-key on the QLineEdit


bool MainWindow::eventFilter(QObject *obj, QEvent *event){
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if ((event->type() == QEvent::KeyPress) && keyEvent->key() == 16777235) {
qDebug("Pressed the up-arrow-key. Should now increase the digit (of the numeric value) which is in front of the IBeam/cursor (on the numeric value associated with the QLineEdit for this filter event)");
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}

The question: Is there some practical method allowing me to increase or decrease the digit in front of the Ibeam on a keystroke event?

d_stranz
3rd August 2018, 18:26
Is there some practical method allowing me to increase or decrease the digit in front of the Ibeam on a keystroke event?

You have a number of QLineEdit methods you can use, like QLineEdit::text() to retrieve the string in the edit box, andQLineEdit::cursorPosition() to tell you where the cursor is. Once you know the string and the cursor position, you can test to see if the character at the cursor position is actually a number (you don't want to increment the decimal point, right?), and if it is, increment or decrement the digit. But unless you want the digits to act like independent digits that you change without affecting the others (like a set of mechanical digit wheels lined up next to each other), then you also need to take into account when a digit rolls past 9 on increment or past 0 on decrement, and change one or more of the other digits in response.

If what you want is for the whole number to change when the value of a single digit changes (i.e. account for rollover / rollunder), then what I would do is to determine which 10s value is represented by the cursor position and add or subtract 10^position to the full number. (In other words, if the cursor is just to the left of the decimal point, you increment by 10^0 (or 1.0). If it is one digit to the right of the decimal point, you increment by 10^-1 (or 0.1). By doing it this way, you don't need to determine -what- the digit is at the cursor position, only what 10s value it represents.

Egeris
3rd August 2018, 18:44
If what you want is for the whole number to change when the value of a single digit changes (i.e. account for rollover / rollunder), then what I would do is to determine which 10s value is represented by the cursor position and add or subtract 10^position to the full number. (In other words, if the cursor is just to the left of the decimal point, you increment by 10^0 (or 1.0). If it is one digit to the right of the decimal point, you increment by 10^-1 (or 0.1). By doing it this way, you don't need to determine -what- the digit is at the cursor position, only what 10s value it represents.
That may be the best way to do it.

Thanks for your input.