PDA

View Full Version : QAbstractSpinBox Subclass exhibits wierd behavour



sunil.thaha
22nd February 2007, 10:38
Hi,

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



class Test : public QAbstractSpinBox {
Q_OBJECT
public:
Test( QWidget *parent =0 )
:QAbstractSpinBox(parent){
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;
t.show();




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.show();
t.setText( "Hi Hi "); // Calls to setText works after show ??


Any clue why this happens?

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

sunil.thaha
22nd February 2007, 11: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 ?

jpn
22nd February 2007, 12: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 *)
{
Q_D(QAbstractSpinBox);

d->reset();
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..

sunil.thaha
22nd February 2007, 12: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 ?

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


void MySpinBox::showEvent(QShowEvent* event)
{
// bypass QAbstractSpinBox::showEvent()
QWidget::showEvent(event);
}

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

sunil.thaha
22nd February 2007, 12:30
Good thinking !!