[Qt-interest] Mismatch between QSpinBox::textFromValue() documentation and actual behavior
K. Frank
kfrank29.c at gmail.com
Wed Mar 24 03:33:59 CET 2010
Anders -
Thanks for the explanation.
On Tue, Mar 23, 2010 at 9:06 PM, Anders Bakken <anders.bakken at nokia.com> wrote:
> On Wed, Mar 24, 2010 at 01:41:48AM +0100, ext K. Frank wrote:
>> ...
>> I find that the behavior of QSpinBox::textFromValue() does not match
>> the documentation.
>> ...
> This is missing from the docs. It does indeed remove it. While this may
> or may not be the most appropriate default it's relatively easy to work
> around.
I think the appropriate default is the current behavior of not having the
thousands separators. (It would, however, be convenient to be able to
turn on the thousands separators with an option flag.)
Just to make sure I understand, is the suggested work-around to derive from
QSpinBox and override textFromValue() (as in the MySpinBox class, below)?
>> At issue is that QWidget::locale().toString(int) formats integers with
>> thousands separators,
>> while QSpinBox::textFromValue(int) does not.
>> ...
>> #include <QSpinBox>
>> class MySpinBox : public QSpinBox {
>> Q_OBJECT
>> public:
>> MySpinBox (QWidget *parent = 0) : QSpinBox (parent) {}
>> ~MySpinBox() {};
>> QString getBaseTextFromValue (int value) const { return
>> QSpinBox::textFromValue (value); }
>> QString getTextFromValue (int value) const { return textFromValue (value); }
>> protected:
>> QString textFromValue (int value) const { return locale().toString
>> (value); }
>> };
>> ...
>> mySpinBox->getBaseTextFromValue (mySpinBox->value()) returns "10000"
>> mySpinBox->locale().toString (mySpinBox->value()) returns "10,000"
>>
>> I would have expected these last two to return the same string because
>> QSpinBox::textFromValue() -- called by MySpinBox::getBaseTextFromValue() --
>> should be equivalent to QWidget::locale().toString().
>>
> The problem is that getBaseTextFromValue explicitly calls
> QSpinBox::textFromValue() which means it will go into this function:
Just to clarify, my purpose in having the getBaseTextFromValue() function was
to access specifically the base-class (QSpinBox) version of textFromValue,
which, as a protected member, cannot be called from outside of a derived class.
That is, I am using getBaseTextFromValue() to probe the protected function
QSpinBox::textFromValue() and verify that it does, indeed, remove the thousands
separators.
> ...
> QString QSpinBox::textFromValue(int value) const
> {
> QString str = locale().toString(value);
> if (qAbs(value) >= 1000 || value == INT_MIN) {
> str.remove(locale().groupSeparator());
> }
>
> return str;
> }
Yes, that would do it...
> I've pushed a change to fix the docs. It could maybe be argued that this
> should be available as an option of some sort.
Thanks for the update to the documentation. (An option would be nice,
but hardly essential.)
> It's relatively simple to work around though.
Just to check: Is the most economical work-around to put in the thousands
separators to override textFromValue() in a derived class?
class MySpinBox : public QSpinBox {
Q_OBJECT
public:
MySpinBox (QWidget *parent = 0) : QSpinBox (parent) {}
~MySpinBox() {};
protected:
QString textFromValue (int value) const { return locale().toString
(value); }
};
> Thanks for the report.
>
> regards
>
> Qt Development Frameworks, Nokia, http://qt.nokia.com
> Anders Bakken
Thanks again for your reply.
K. Frank
More information about the Qt-interest-old
mailing list