QAbstractSpinBox Subclass exhibits wierd behavour
Hi,
Does anyone know why this simple class exihits a wierd behavior ?
Code:
Q_OBJECT
public:
lineEdit()->setText( "Hello " );
}
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
Now I tried this, note that the setText() call after the show works, but not before it ?
Code:
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?
Re: QAbstractSpinBox Subclass exhibits wierd behavour
Do you want to just show the text instead of numbers? You may reimplement textFromValue from QSpinBox.
Re: QAbstractSpinBox Subclass exhibits wierd behavour
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 ?
Re: QAbstractSpinBox Subclass exhibits wierd behavour
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:
Code:
// from qabstractspinbox.cpp:
{
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..
Re: QAbstractSpinBox Subclass exhibits wierd behavour
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 ?
Re: QAbstractSpinBox Subclass exhibits wierd behavour
The particular problem can be avoided by bypassing QAbstractSpinBox::showEvent():
Code:
{
// bypass QAbstractSpinBox::showEvent()
}
Edit: I'm not 100% sure whether it will break something else, though ;)
Re: QAbstractSpinBox Subclass exhibits wierd behavour