PDA

View Full Version : Dialog and code design issue



Gopala Krishna
24th September 2006, 16:08
Hello friends,
I am porting an app not originally coded by me from Qt3 to Qt4. I am also improving the readability and design of code. While porting I came across a design issue. The problem is with the dialog whose pic is attached. It actally uses pure c++ (ported as it is from c without using signal-slot mechanism) as backend for the calculations.
Whenever the the value in leftmost combobox(in Transmission Line Type group box) is changed there are corresponding changes. The changes include disabling or hiding of some comboboxes,linedit and labels (some rows of widgets). This results in changes in sizes of child widgets as well as dialog which I don't want to happen.
This is achieved by using many structures which holds pointers to comboboxes, lineedits, labels.. Other than this lots of instances of layouts ,Q*Boxes,Q*GroupBoxes are used to manage this. This has really increased my problems in porting since Qt4 doen't have QVBox,QHBox,QVGroupBox,QHGroupBox forcing me to manage layouts manually.
So, I thought I can create a widget to manage these properties,units,combo boxes and line edits in much easier way. I have attached the code for this. It works(but I haven't tested it much)
But I think there exists a cleaner solution for this. Especially I think I can incorporate model-view design to this and make the code simpler.
So, I request your suggestions regarding

Design of dialog(placement of widget)
any other qt4 widget alternatives
model-view concerning this
Overall design of dialog (managing availability and inavailability of widgets)

Thanks for your time.

jacek
24th September 2006, 18:54
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 units();
static QStringList similarUnits( const QString& unit );dir

...
};
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 {
....
};
QString toString( Unit unit );
QList<Unit> units();
QList<Unit> similarUnits( Unit unit );
...
};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:
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 );
...
};
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.