[Qt-interest] aspect-ratio for graphs

Sean Harmer sean.harmer at maps-technology.com
Thu Jul 15 13:44:22 CEST 2010


On Thursday 15 July 2010 11:55:15 phil prentice wrote:
> Sorry Sean
>   Finger/Brain trouble...

No worries.

>   Indeed I am trying to use heightForWidth()...See my original mail which
> has a derived object within a layout.  I am cutting it down though and
> will continue to try and get it to work.  If no success I will mail the
> cut down version of my code. If I am successful I will also provide the
> code. Thanks for your help

OK I have got an example of how to do this for you - see attached tarball.

The key is to set a suitable size policy in your custom widget (be it derived 
from QWidget or from QGraphicsView) and to implement the int heightForWidth( 
int w ) function.

In the attached example I derived a class FixedAspectWidget from QWidget. The 
ctor and heightForWidth() functions look like this:

FixedAspectWidget::FixedAspectWidget(QWidget *parent) :
    QFrame(parent)
{
    setMinimumSize( 100, 100 );
    QSizePolicy policy( QSizePolicy::Preferred, QSizePolicy::Minimum );
    policy.setHeightForWidth( true );
    setSizePolicy( policy );
}

int FixedAspectWidget::heightForWidth(int w) const
{
    return w;
}

That is you have to call QSizePolicy::setHeightForWidth( true ). This then 
tells any layout into which you place your widget that the layout needs to 
call your widget's heightForWidth() function which in this case simply returns 
the input width resulting in a square widget.

This of course only provides you with a widget of fixed aspect ratio, drawing 
your graphs with a fixed aspect ratio is a whole other problem when you 
consider axis labels, ticks, titles, different coordinate systems etc.

HTH,

Sean

> Phil
> 
> On Thursday 15 July 2010 10:34, you wrote:
> > Hi Phil,
> > 
> > please reply to the list not to me personally.
> > 
> > On Thursday 15 July 2010 11:22:10 phil prentice wrote:
> > > Thanks Sean
> > > 
> > >   Will it help me with my aspect-ratio problem....because if it does
> > >   then
> > > 
> > > I would definitely use it.  Its the aspect-ratio problem I would like
> > > to solve...I need my graphs to have aspect-ratio of 1:1(inside a
> > > layout).  I could not see how QGraphicsView can help me there??? 
> > > Please tell me that I am wrong!!!
> > 
> > QGraphicsView itself is just a QWidget so it will not support fixed
> > aspect ratio of the widget itself out of the box - you'll still need to
> > use something like heightForWidth() to implement that.
> > 
> > You can make the graphs within your scene fixed aspect ratio quite easily
> > though. Can you provide a .ui file that shows the kind of layout you
> > would like?
> > 
> > > I am still trying to play, but as of yet not getting anywhere
> > 
> > I have no time to look at this right now but I may be able to later.
> > 
> > Cheers,
> > 
> > Sean
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fixed-aspect.tar.gz
Type: application/x-compressed-tar
Size: 1589 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100715/2e416120/attachment.bin 


More information about the Qt-interest-old mailing list