View Full Version : QAbstractSpinBox Subclass exhibits wierd behavour

22nd February 2007, 11:38

Does anyone know why this simple class exihits a wierd behavior ?

class Test : public QAbstractSpinBox {
Test( QWidget *parent =0 )
lineEdit()->setText( "Hello " );

void setText( const QString &txt){
lineEdit()->setText( txt );

I have subclassed a QAbstractSpinBox and in the constructor I have set the SpinBox's lineedit's text to "Hello" So In the code below I expect to see the text "Hello " inside the LineEdit but It remains blank

Test t;

Now I tried this, note that the setText() call after the show works, but not before it ?

Test t;
t.setText( "Before show"); // Will not work, to test it comment the setText below show
t.setText( "Hi Hi "); // Calls to setText works after show ??

Any clue why this happens?

22nd February 2007, 11:43
Do you want to just show the text instead of numbers? You may reimplement textFromValue from QSpinBox.

22nd February 2007, 12:38
I am Subclassing QAbstractSpinBox, My intension is to make a widget like QTimeEdit with a different functionality. But this does not explain why the setText() works after show() gets called ?

22nd February 2007, 13:01
I recall someone else getting bumped into this problem too. The problem lies within QAsbtractSpinBox::showEvent() which updates the content of the line edit like this:

// from qabstractspinbox.cpp:
void QAbstractSpinBox::showEvent(QShowEvent *)

d->updateEdit(); // <-- this is the one that causes the problem

The problem is that QAbstractSpinBoxPrivate::updateEdit() uses QAbstractSpinBoxPrivate::textFromValue(). In QAbstractSpinBox subclasses like QSpinBox and QDoubleSpinBox the corresponding pimpl method is overridden and the call is redirected to the corresponding method in the public implementation class, QSpinBox::textFromValue() and so on. In my humble opinion this is a little design flaw by the Trolls..

22nd February 2007, 13:16
Does that mean that QAbstractSpinBox is never meant to be overridden by users, since its private implementation is not documented? I am developing a widget like the QTimeEdit with one more field, like the frame, and the value of the frame can be anywhere from 0 - (frameRate -1). So is QDateTimeEdit or QTimeEdit a good candidate to override ?

22nd February 2007, 13:24
The particular problem can be avoided by bypassing QAbstractSpinBox::showEvent():

void MySpinBox::showEvent(QShowEvent* event)
// bypass QAbstractSpinBox::showEvent()

Edit: I'm not 100% sure whether it will break something else, though ;)

22nd February 2007, 13:30
Good thinking !!