[Qt-interest] Change layout in subclass of standard widget ?
Andreas Pakulat
apaku at gmx.de
Tue May 26 14:47:46 CEST 2009
On 26.05.09 10:52:37, Stephen Collyer wrote:
> 2009/5/26 Tony Rietwyk <tony.rietwyk at rightsoft.com.au>
> > Stephen wrote:
> >
> > How does one go about changing the layout of a derived standard
> > widget in this case ? I'm doing something wrong, but can't see how
> > to fix it.
> >
> > The internal layout of the standard widgets is private to them, so other
> > than the public API, you can only modifiy the original source code to modify
> > the private layout - which is not desirable.
> >
>
> This doesn't seem entirely correct to me. All classes derived from QWidget
> support setLayout() so it would seem possible, at least in principle,
> to change the layout of a standard widget. The QWidget docs give an
> example of this being done:
>
> http://doc.qtsoftware.com/4.5/qwidget.html#setLayout
>
> I suspect I'm merely doing it incorrectly.
No, what you're missing is that a QTableView doesn't actually have an
internal layout. It is completely drawing its scrollarea itself. So if you
start to set layouts on this widget, you're breaking tons of assumptions
done when the QTableView code was written.
So subclassing and changing layouts works for widgets that use existing
standard widgets in a layout, but won't work for widgets that draw their
content themselves (or have internal classes to represent items that they
layout out completely themselves outside of the setLayout functionality).
> > In this case, I suggest a separate widget that contains the table view and
> > buttons, and that connects to the button clicks and does the appropriate
> > actions on the table. The separate widget can also listen for the view
> > selection signals, and only enable the buttons accordingly.
>
> I could do that, but it's a totally different design and much less elegant
> than merely deriving from the view widget.
IMHO you're wrong here, because your widget is not a tableview anymore. Its
a tableview+something extra. So in fact the proper relation of your widget
to the tableview should be a has-A relation, not is-A.
Andreas
--
So this is it. We're going to die.
More information about the Qt-interest-old
mailing list