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

Nathan Carter nathancarter5 at gmail.com
Fri Mar 27 20:47:22 CET 2009


That creates lots of unnecessary widgets, especially if Helge decides  
to increase the number of types, and creates dozens of these.

How about changing the relationship from is-a to has-a?  Just make  
your ParameterInput inherit from QHBox instead, and put the  
appropriate input widget inside, based on the type?

You should also consider just making one class, not many, and using an  
enum for types, and returning QVariants.  (They already have the type  
enum built-in.)  Something like this:

class ParameterInput : public QHBox
{
private:
	QVariant::Type type;
	QWidget* inner;
public:
	ParameterInput ( QVariant::Type type = QVariant::Int, QWidget* parent  
= 0 )
		: QWidget( parent ), type( type )
	{
		switch ( type )
		{
			case QVariant::Int:
				inner = new QSpinBox( ..., this );
				break;
			case ...:
			...
		}
	};
	QVariant value ()
	{
		switch ( type )
		{
			case QVariant::Int:
				return qobject_cast<QSpinBox*>( inner )->value();
			case ...:
			...
		}

	};
};

You could even later have a signal emitted on data change, and hook it  
up to the corresponding slot of inner on its construction, etc.   
Furthermore, you could add some other convenience methods, like  
ParameterInput(QVariant v,QWidget* parent) and setValue(QVariant v)  
and so forth.  All done with switch statements.

Except for this idea:  Do all the widgets you're interested in using  
have the value property equal to what you want?  If so, throw the  
switch statements out the window and just use setProperty() and  
getProperty() on the value property.

Surely someone has done this somewhere already; I'm getting the  
reinventing the wheel feeling.  Anyone?...anyone?...Bueller?

Nathan


On Mar 27, 2009, at 3:33 PM, ygor at comcast.net wrote:

> How about a QStackedLayout ?
>
> http://doc.trolltech.com/4.5/qstackedlayout.html
>
> One "page" per parameter type.
>
> ----- 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
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list