[Qt-interest] Generic input widgets for varying types - how to?
Helge Preuss
helge.preuss at gmx.net
Fri Mar 27 20:18:05 CET 2009
What I am trying to achieve:
I need a widget which can serve as input widget for several types. For
example, if the parameter read from the widget is an integer, it should
display a QSpinBox. If it's a string, a QLineEdit. There are other
types, but I'll limit myself to these.
How I am trying to do it:
(sorry if I'm spamming code here, this example is as concise as I could
make it.)
class ParameterInput: public QWidget {
public:
virtual QString value() const = 0;
virtual void setValue(const QString &) = 0;
};
class ParameterLineEdit: public ParameterInput, public QLineEdit {
public:
virtual QString value() const { return QLineEdit::text(); }
virtual void setValue(const QString &s) { QLineEdit::setText(s); }
};
class ParameterSpinBox: public ParameterInput, public QSpinBox {
public:
virtual QString value() const { return
QString::number(QSpinBox::value()); }
virtual void setValue(const QString &s) {
QSpinBox::setValue(s.toInt()); }
};
class ParameterInputFactory {
public:
static ParameterInput *create(const QString &, QWidget * = 0);
static ParameterInput *create(int, QWidget * = 0);
};
// The uic-generated header file for a Dialog with ParameterInputs of
varying type
class ValuesDialog {
...
// Manually edited uic output here:
ParameterInput * someParameter;
ParameterInput * otherParameter;
};
// Constructor for the implementation of the ValuesDialog
ValuesDialogImpl::ValuesDialogImpl() {
// ...
int i;
QString s;
someParameter = ParameterInputFactory::create(i, this);
otherParameter = ParameterInputFactory::create(s, this);
}
Question 1:
Is this a good way to do it? Differently put, is there a /better/ way
;-) ? In particular, I am a bit concerned about deriving the concrete
input widgets from QWidget twice - once via ParameterInput and once via
QSlider/QLineEdit.
Question 2:
This does not really work. The ParameterInput widgets are not displayed.
I assume I must implement some QWidget methods to make the widgets
display. Which methods would that be?
Greets and thanks,
Helge
--
Helge Preuss
Freelance Software Developer
+49 30 40 30 10 90
+49 177 2262 484
helge.preuss at gmx.net
More information about the Qt-interest-old
mailing list