[Qt-interest] Generic input widgets for varying types - how to?

Helge Preuss helge.preuss at gmx.net
Fri Mar 27 20:56:02 CET 2009


ygor at comcast.net wrote:
> How about a QStackedLayout ?
>  
> http://doc.trolltech.com/4.5/qstackedlayout.html
>
> One "page" per parameter type.
>   
Thanks for the idea, but there are a few things I'm not comfortable with.

First, I'd have to create a page in the layout for every type of input
parameter I use. That's going to be quite a few. Seems wasteful to me,
and also it seems I'd have to edit code in more than one place if I add
a new type.
Second, it is not clear from the documentation if the layout changes
size when a different page is shown. It may (which would be right for
me). I'd have to check.
Third, it kinda seems like a hack. I'm looking for a way to do this
/right/. The QItemEditorFactory sounds like it's the "canonic" way in Qt.

Greets,

Helge
> ----- Original Message -----
> From: Helge Preuss <helge.preuss at gmx.net>
> To: qt-interest at trolltech.com
> Sent: Fri, 27 Mar 2009 19:18:05 +0000 (UTC)
> Subject: [Qt-interest] Generic input widgets for varying types - how to?
>
> 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