[Qt-interest] Howto reimplement qdoublespinbox with exponential scientific expressions
IP Susila
putu-sgr at ecei.tohoku.ac.jp
Wed Dec 3 12:12:41 CET 2008
(2008/12/01 1:53), Matthias Pospiech wrote:
> Unfortunately Qt offers at not widget the possibility to edit
> exponetial values. I would like to have the functionality of
> QDoubleLineEdit with the extension of Scientific Expressions.
>
> I have been looking at the source and found the following functions to
> be virtual, so that I can overwrite them:
>
> virtual QValidator::State validate(QString &input, int &pos) const;
> virtual double valueFromText(const QString &text) const;
> virtual QString textFromValue(double val) const;
> virtual void fixup(QString &str) const;
>
> However
>
> double round(double input) const;
>
> from QDoubleSpinBoxPrivate is not virtual:
>
> double QDoubleSpinBoxPrivate ::round(double value) const
> {
> Q_Q(const QDoubleSpinBox);
> const QString strDbl = q->locale().toString(value, 'f', decimals);
> return q->locale().toDouble(strDbl);
> }
>
> It is used in setValue:
>
> void QDoubleSpinBox::setValue(double value)
> {
> Q_D(QDoubleSpinBox);
> QVariant v(d->round(value)); // round according to decimals
> d->setValue(v, EmitIfChanged);
> }
>
> this unfortunately would round up the digits, even if the value would
> be 1e-20, which would become 0 at decimals = 4.
> Unaccaptable for my intention. I instead want 1.02349e-20 to become
> 1.0235e-20 at the round function.
>
> Instead of creating QScienceSpinBox from QDoubleSpinBox I tried to
> copy the source and rename it. However then I deal with all the
> internal implementations of Qt, which might change. So I am stuck and
> do not know how to proceed.
>
> Any ideas how to implement such a QScienceSpinBox , except from
> reimplementing QDoubleSpinBox totally?
>
> Matthias Pospiech
>
Hello Matthias,
I've tried to implement such kind of spin box.
Basically I just set the 'decimals' to big number (e.g. 1000, etc),
which will affect
the precision of the data being stored to 'value'. Then I override
'textFromValue'
to convert the 'value' into proper format (display format, number of
decimal points etc).
Please see the attached code for demo.
The implementation and demo are very simple. I hope you get some hints
from it.
regards,
Putu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: qscispinbox.zip
Type: application/octet-stream
Size: 2996 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20081203/aa8942e3/attachment.obj
More information about the Qt-interest-old
mailing list