Results 1 to 9 of 9

Thread: Disable keyboard input for spinbox entry

  1. #1
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Question Disable keyboard input for spinbox entry

    Qt Code:
    1. QSpinBox *spinBox ;
    2. spinBox = new QSpinBox;
    To copy to clipboard, switch view to plain text mode 


    There is no setValidator for QSpinBox like as for QLineEdit.
    I want to restrict the user's input to clicking up and down arrows only,
    and not allow user to type a value into the spin box's line editor.
    How to do ?


    S.O.S

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Disable keyboard input for spinbox entry

    You can subclass QSpinBox, and reimplement keyPressEvent(QKeyEvent *event).
    Use QKeyEvent::ignore() to ignore the event.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Disable keyboard input for spinbox entry

    Or alternatively you can use event filters

  4. #4
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable keyboard input for spinbox entry

    I'm not really clear on how to do it exactly? where can i get help or some sample code?

  5. #5
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable keyboard input for spinbox entry

    Quote Originally Posted by saa7_go View Post
    You can subclass QSpinBox, and reimplement keyPressEvent(QKeyEvent *event).
    Use QKeyEvent::ignore() to ignore the event.


    Hi.What is subclass QSpinBox and how to do it exactly?
    Thank you.

  6. #6
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Disable keyboard input for spinbox entry

    Quote Originally Posted by babygal View Post
    Hi.What is subclass QSpinBox and how to do it exactly?
    You create a new class based on QSpinBox. For example:

    Qt Code:
    1. #include <QSpinBox>
    2. #include <QKeyEvent>
    3.  
    4. class SpinBox : public QSpinBox
    5. {
    6. public:
    7. SpinBox(QWidget *parent = 0) : QSpinBox(parent) { }
    8.  
    9. protected:
    10. // reimplement keyPressEvent
    11. void keyPressEvent(QKeyEvent *event)
    12. {
    13. event->ignore();
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by saa7_go; 31st August 2010 at 07:20. Reason: updated contents

  7. The following user says thank you to saa7_go for this useful post:

    babygal (31st August 2010)

  8. #7
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable keyboard input for spinbox entry

    Quote Originally Posted by saa7_go View Post
    You create a new class based on QSpinBox. For example:

    Qt Code:
    1. #include <QSpinBox>
    2. #include <QKeyEvent>
    3.  
    4. class SpinBox : public QSpinBox
    5. {
    6. public:
    7. SpinBox(QWidget *parent = 0) : QSpinBox(parent) { }
    8.  
    9. protected:
    10. // reimplement keyPressEvent
    11. void keyPressEvent(QKeyEvent *event)
    12. {
    13. event->ignore();
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 
    I just implemented as above and it works!.. Yaayy...Thanks a million!

  9. #8
    Join Date
    Mar 2016
    Location
    Syracuse, NY, USA
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Disable keyboard input for spinbox entry

    I realize this is an old thread, but another option is to make the QLineEdit with in the spin box read only.

    Qt Code:
    1. class tcNoEditSpinBox : public QSpinBox
    2. {
    3. Q_OBJECT
    4. public:
    5. tcNoEditSpinBox (QWidget *parent) : QSpinBox (parent)
    6. { lineEdit()->setReadOnly (true); }
    7. };
    8.  
    9. class tcNoEditDoubleSpinBox : public QDoubleSpinBox
    10. {
    11. Q_OBJECT
    12. public:
    13. tcNoEditDoubleSpinBox (QWidget *parent) : QDoubleSpinBox (parent)
    14. { lineEdit()->setReadOnly (true); }
    15. };
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Disable keyboard input for spinbox entry

    Subclassing is unnecessary. QAbstractSpinBox, from which other spin box classes are derived, has a QAbstractSpinBox::setReadOnly() method which accomplishes what the OP wanted.
    <=== 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.

Similar Threads

  1. Why modeless dialog block keyboard input
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2010, 02:08
  2. How to disable keyboard
    By garry_3peace in forum Newbie
    Replies: 9
    Last Post: 27th October 2009, 04:17
  3. Replies: 5
    Last Post: 17th July 2009, 12:17
  4. Accessing keyboard input in child process
    By GTBuilder in forum Qt Programming
    Replies: 0
    Last Post: 22nd January 2008, 16:04
  5. How to enable keyboard and mouse as input in qtopia-2.1.1
    By hvreddy1110 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 12th October 2006, 10:30

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.