View Full Version : Disable keyboard input for spinbox entry
babygal
30th August 2010, 10:49
QSpinBox *spinBox ;
spinBox = new QSpinBox;
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
saa7_go
30th August 2010, 11:44
You can subclass QSpinBox, and reimplement keyPressEvent(QKeyEvent *event).
Use QKeyEvent::ignore() to ignore the event.
aamer4yu
30th August 2010, 13:07
Or alternatively you can use event filters
babygal
31st August 2010, 06:43
I'm not really clear on how to do it exactly? where can i get help or some sample code?
babygal
31st August 2010, 06:44
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.
saa7_go
31st August 2010, 07:16
Hi.What is subclass QSpinBox and how to do it exactly?
You create a new class based on QSpinBox. For example:
#include <QSpinBox>
#include <QKeyEvent>
class SpinBox : public QSpinBox
{
public:
SpinBox(QWidget *parent = 0) : QSpinBox(parent) { }
protected:
// reimplement keyPressEvent
void keyPressEvent(QKeyEvent *event)
{
event->ignore();
}
};
babygal
31st August 2010, 08:36
You create a new class based on QSpinBox. For example:
#include <QSpinBox>
#include <QKeyEvent>
class SpinBox : public QSpinBox
{
public:
SpinBox(QWidget *parent = 0) : QSpinBox(parent) { }
protected:
// reimplement keyPressEvent
void keyPressEvent(QKeyEvent *event)
{
event->ignore();
}
};
I just implemented as above and it works!.. Yaayy...Thanks a million!
Judge Gazza
1st December 2016, 23:07
I realize this is an old thread, but another option is to make the QLineEdit with in the spin box read only.
class tcNoEditSpinBox : public QSpinBox
{
Q_OBJECT
public:
tcNoEditSpinBox (QWidget *parent) : QSpinBox (parent)
{ lineEdit()->setReadOnly (true); }
};
class tcNoEditDoubleSpinBox : public QDoubleSpinBox
{
Q_OBJECT
public:
tcNoEditDoubleSpinBox (QWidget *parent) : QDoubleSpinBox (parent)
{ lineEdit()->setReadOnly (true); }
};
d_stranz
2nd December 2016, 01:06
Subclassing is unnecessary. QAbstractSpinBox, from which other spin box classes are derived, has a QAbstractSpinBox::setReadOnly() method which accomplishes what the OP wanted.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.