Hallo ihr Lieben.
Ich schreibe ein weiteres mal, weil ich auf eure Hilfe hoffe.
Ich möchte eine QSpinbox benutzen, allerdings dabei mit Float-Zahlen umgehen.
Um dies zu tun, muss man eine Unterklasse zu QSpinbox schreiben. Auch wenn dies eine ganz einfache Sache sein soll, so kriege ich es bei mir nicht recht hin.
Da habe ich aus dem Netz ein recht gut laufende FloatSpinbox verwendet. Diese mach jedoch einen Fehler:
Will ich direkt einen Wert in das Fenster des Spinbox eingeben, so passieren bei einigen Werten Rundungsfehler.
Z.B. wird aus einer 2.3 eine 2.2 oder aus einer 2.1 eine 2.0.
Ist halt ziemlich blöd wenn man eine 2.3 einträgt und auf einmal ne 2.2 in dem Eingabebereich steht.
Benutzt man die "Pfeile" um einen Schritt hoch/runter zu gehen, so läuft es wunderbar.
Seht ihr den Fehler?
Grüße!
Erstellen tue ich mir die FloatSpinbox dann zB so:
FloatSpinBox* fBox = new FloatSpinBox(0.0, 10.0,1, 0.1, 3.5, hbox);
#include <qspinbox.h>
#include <math.h>
FloatSpinBox::FloatSpinBox( QWidget* parent, const char* name )
: QSpinBox( int(-2e10), int(2e10), 10, parent, name )
{
dVal = new QDoubleValidator( -2e8, 2e8, 2, this, "dVal" );
setValidator(dVal);
setValue( 0.0 );
}
/////////////////////////////////////////////////////////////////////
FloatSpinBox::FloatSpinBox( float minValue, float maxValue, int decimals,
float step, float startValue, QWidget* parent, const char* name )
: QSpinBox( int( minValue * pow( 10, decimals ) ), int( maxValue * pow( 10, decimals ) ),
int( rint( step * pow( 10, decimals ) ) ), parent, name ),
dec( decimals )
{
dVal = new QDoubleValidator( minValue, maxValue, 2, this, "dVal" );
setValidator(dVal);
setValue(startValue);
}
/////////////////////////////////////////////////////////////////////
FloatSpinBox::~FloatSpinBox()
{
}
/////////////////////////////////////////////////////////////////////
QString FloatSpinBox::mapValueToText ( int value )
{
QString s;
s.setNum( float( value )/ pow( 10, dec ), 'f', dec );
return s;
}
/////////////////////////////////////////////////////////////////////
int FloatSpinBox::mapTextToValue ( bool * ok )
{
return int( cleanText().toFloat( ok ) * pow( 10, dec ) );
}
/////////////////////////////////////////////////////////////////////
float FloatSpinBox::value() const
{
return float( QRangeControl::value() ) / pow( 10, dec );
}
/////////////////////////////////////////////////////////////////////
float FloatSpinBox::minValue() const
{
return float( QRangeControl::minValue() ) / pow( 10, dec );
}
/////////////////////////////////////////////////////////////////////
float FloatSpinBox::maxValue() const
{
return float( QRangeControl::maxValue() ) / pow( 10, dec );
}
/////////////////////////////////////////////////////////////////////
void FloatSpinBox::setValue( float value )
{
QRangeControl::setValue( int( value * pow( 10, dec ) ) );
}
/////////////////////////////////////////////////////////////////////
void FloatSpinBox::setRange( float minValue, float maxValue )
{
QRangeControl::setRange( int( minValue * pow( 10, dec ) ),
int( maxValue * pow( 10, dec ) ) );
dVal->setRange( minValue, maxValue, 2 );
}
/////////////////////////////////////////////////////////////////////
void FloatSpinBox::valueChange()
{
QSpinBox::valueChange();
emit valueChanged( value() );
}
Problem mit Float Spinbox
-
Flachkoepper
- Beiträge: 149
- Registriert: 11. Januar 2005 12:14
- Wohnort: Hannover
-
Flachkoepper
- Beiträge: 149
- Registriert: 11. Januar 2005 12:14
- Wohnort: Hannover
-
Flachkoepper
- Beiträge: 149
- Registriert: 11. Januar 2005 12:14
- Wohnort: Hannover