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

Andreas Pakulat apaku at gmx.de
Fri Mar 27 22:35:37 CET 2009


On 27.03.09 22:00:55, Helge Preuss wrote:
> Andreas Pakulat wrote:
> >> Uh-oh. How would I make a dialog contain a ParameterInput then? It must
> >> be a QWidget, else it cannot be a sub-widget, am I right?
> >>     
> >
> > That depends on what you actually want to achieve. I just had another
> > look at your initial mail. The part about manually editing a uic
> > generated file is problematic. It basically makes using the
> > uic-generated file completely meaningless, the whole point of doing that
> > is to easily update the gui without having to touch code, which is not
> > possible with your idea.
> >   
> /me veers off topic:
> 
> I'm not sure about that... I used the Designer to easily build an
> interface, which I probably won't have to change manually. Designer/uic
> is just not powerful enough to have a variable QWidget in one place, the
> way I intend to. Or am I mistaken?

Right, thats why I said you should just write the gui-creation code
yourself instead of "pretending" to use designer/uic when you actually
don't really do it.
 
> Anyway, I just used the Designer to spare me the tedium of hand-writing
> all the code to create the GUI. If I have to change the code manually
> later, it will still have spared me that.

Then I'd suggest to simply copy the setupUi code into the constructor of
your mainwindow/widget.

> > So you could just do
> > QWidget* inputWigdet = Factory::create( LineEdit );
> > dynamic_cast<ParameterInput*>(inputWidget)->setValue( "SomeString" );
> >
> > Or even pass around the "widget" only by using the ParameterInput
> > pointer so no code outside the place that actually creates the widgets
> > from the factory sees which widget is behind the ParameterInput.
> >   
> I'm not quite sure whether this is possible for me, but I'll give your
> idea a test ride.

Well, wether its possible or not simply depends on what you're designing
this interface for, i.e. whats the intended use for it. So far my
understanding was that code that uses that interface simply shouldn't
care how the data it supplies is visualized, in which case it would make
perfect sense to only pass around a pointer to the interface and not a
QWidget* which would then possibly be casted down to the actual
widget...

> >>>> 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?
> >>>>     
> >>>>         
> >>> I suggest to fix the above first and then revisit this, all kinds of
> >>> weird things may happen when you subclass twice from qwidget. In
> >>> particular in your case probably the paintEvent of QWidget is being
> >>> called (just painting a grey background) instead of the one from the
> >>> actual widgets.
> >>>   
> >>>       
> >> That's probably what happens, yes.
> >>
> >> So if my above hunch about ParameterInput having to be a QWidget is
> >> correct, I'll probably have to use composition in the derived classes.
> >> Such as:
> >>
> >> class ParameterLineEdit: public ParameterInput {
> >>   private:
> >>     QLineEdit *delegateLineEdit;
> >>     void setValue(const QString &s) { delegateLineEdit->setText(s); }
> >>     //  etc.
> >> }
> >>     
> >
> > Well, you'd need the delegating for setValue/value anyway, but more
> > importantly in this case you'd need to delegate the whole QLineEdit API
> > to make it work. Thats going to be a lot of work for no gain (that I can
> > currently see).
> >   
> It's not the /whole/ QLineEdit interface (the ParameterInput interface
> just contains get/setValue()), but apparently those parts that make the
> QLineEdit displayable correctly (that is kind of what question 2 in my
> original post was aiming at - which functions would that be?). But your
> point is taken.

Well, you'd need to replicate most, possibly all of the public+protected
API in QWidget to make it display and behave properly (especially all
the event handlers).

Andreas

-- 
Try to value useful qualities in one who loves you.



More information about the Qt-interest-old mailing list