Maybe you could create a class named Value (or similar) that would represent a floating point value with an unit. Something like:
class Value
{
public:
Value( double value, const QString& unit = "" );
double value() const;
const QString& unit() const;
...
Value convertTo( const QString& otherUnit );
static QStringList similarUnits
( const QString
& unit
);dir
...
};
class Value
{
public:
Value( double value, const QString& unit = "" );
double value() const;
const QString& unit() const;
...
Value convertTo( const QString& otherUnit );
static QStringList units();
static QStringList similarUnits( const QString& unit );dir
...
};
To copy to clipboard, switch view to plain text mode
or
class Value
{
public:
Value( double value, Units::Unit unit = Units::None );
double value() const;
Units::Unit unit() const;
...
Value convertTo( Unit otherUnit );
...
};
namespace Units
{
enum Unit {
....
};
QList<Unit> units();
QList<Unit> similarUnits( Unit unit );
...
};
class Value
{
public:
Value( double value, Units::Unit unit = Units::None );
double value() const;
Units::Unit unit() const;
...
Value convertTo( Unit otherUnit );
...
};
namespace Units
{
enum Unit {
....
};
QString toString( Unit unit );
QList<Unit> units();
QList<Unit> similarUnits( Unit unit );
...
};
To copy to clipboard, switch view to plain text mode
or whatever. This should should handle information about available units and conversions between them.
Another thing you might try is a custom widget derived from QGroupBox. Something like:
{
...
void addParameter( const QString& label, const Value& defaultValue );
void setValue( const QString& parameter, const Value& value );
Value value( const QString& parameter );
void setParameterEnabled( const QString& label, bool enabled );
...
};
class ParameterBox : public QGroupBox
{
...
void addParameter( const QString& label, const Value& defaultValue );
void setValue( const QString& parameter, const Value& value );
Value value( const QString& parameter );
void setParameterEnabled( const QString& label, bool enabled );
...
};
To copy to clipboard, switch view to plain text mode
ParameterBox::addParameter() should add a QLabel, QLineEdit and QComboBox to a QGridLayout and populate them with data. You will also need some signals, like valueChanged( const QString& parameter, const Value& value ).
You can also try a Parameter class, which would contain information about parameter name, its value and units, so you won't have to use parameter name and value separately. This way you can have an object that gives you a list of parameters to display and use signals and slots to handle changes. For example Parameter::valueChanged() would cause the widget to update its data and when user edits the the value you can just invoke Parameter::setValue() or Parameter::setUnit() slot.
In Qt 4.2 there is QDataWidgetMapper. You can also write your own view widget and use a model derived from QAbstractTableModel with three columns: name, value and unit.
There are countless possibilities.
Bookmarks