Results 1 to 3 of 3

Thread: Edit digit at cursor position in a QlineEdit

  1. #1
    Join Date
    Aug 2018
    Posts
    2
    Thanks
    1
    Qt products
    Qt5

    Question Edit digit at cursor position in a QlineEdit

    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:
    IBeam.png

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

    Qt Code:
    1. bool MainWindow::eventFilter(QObject *obj, QEvent *event){
    2. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    3. if ((event->type() == QEvent::KeyPress) && keyEvent->key() == 16777235) {
    4. 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)");
    5. return true;
    6. } else {
    7. // standard event processing
    8. return QObject::eventFilter(obj, event);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    The question: Is there some practical method allowing me to increase or decrease the digit in front of the Ibeam on a keystroke event?
    Last edited by Egeris; 3rd August 2018 at 18:22.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Edit digit at cursor position in a QlineEdit

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Egeris (3rd August 2018)

  4. #3
    Join Date
    Aug 2018
    Posts
    2
    Thanks
    1
    Qt products
    Qt5

    Default Re: Edit digit at cursor position in a QlineEdit

    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.

Similar Threads

  1. Control the position of the icon in a QLineEdit
    By PinTxO in forum Qt Programming
    Replies: 0
    Last Post: 7th September 2016, 11:06
  2. New Register
    By mouni in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2015, 10:13
  3. What is register int?
    By kiboi in forum General Programming
    Replies: 2
    Last Post: 7th November 2012, 00:27
  4. set cursor position in qlineedit with an inputmask
    By guitar1 in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2011, 18:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.