PDA

View Full Version : QAbstractSpinBox subclassing



nowire75
28th September 2007, 23:58
Hi,

I want to make my personal speedbox for set a variable type like this:

X 0000, Y 0000, Z 0000, A 000, B 000, C 000

where X,Y,Z can be change from -9999 to 9999 and A,B,C can be change from -270 to 270.

I have write this code

miospinbox.h


#include <QWidget>
#include <QAbstractSpinBox>
#include <QLineEdit>
#include <QValidator>

class miospinbox : public QAbstractSpinBox
{
Q_OBJECT

public:
miospinbox();

};


and miospinbox.cpp



#include "miospinbox.h"

miospinbox::miospinbox()
{
QLineEdit *lineedit = new QLineEdit(this);
//QRegExp rx("-?\\d{1,3}");
QRegExp rx("[1-9]\\d{0,3}");
QValidator *validator = new QRegExpValidator(rx, this);

lineedit->setInputMask("\\X 0000\\, Y 0000, Z 0000, \\A 000, \\B 000, \\C 000;0");
lineedit->setValidator(validator);
setLineEdit(lineedit);
}


The validator in not ok, I see this, but setInputMask I think is ok.
When I show the myspinbox on the window, I'can not change the number with keyboard and I can not change with the mouse clicking on arrow of the spinbox.

wysota
29th September 2007, 00:57
It won't work that way as the spinbox will try to change the value to int, resulting in always obtaining 0. You have to reimplement more methods (at least ones converting between text and value) to do what you require.

nowire75
29th September 2007, 09:46
Thank you

where I can find a example?

wysota
29th September 2007, 10:08
Hmm... actually it looks like I was wrong. You have to reimplement stepBy(). Oh, and you don't have to set a new lineedit to the widget, you can use the one that is already there.