[Qt-interest] aspect-ratio for graphs

phil prentice philp.cheer at talktalk.net
Thu Jul 15 14:23:56 CEST 2010


Hi
  I have cut down my original code to something more manageable but still 
unfortunately not getting anywhere.  I have not got a .ui file. I am 
programming layout in my code. My example is very simple; I am trying to 
allow expansion in both horizontal and vertical directions. I am using 
heightForWidth() and setSizePolicy() to try and achieve a 1:1 ratio.

I've tried putting the heightForWidth() and setSizePolicy() in different 
places, but does not work for me.  If this mechanism is meant to work then I 
am probably doing something stupid.

Any help welcome
Thanks
Phil

Code:- Derived object of QFrame.
VIgraph.h
--------------
#include <QFrame>
class VIgraph : public QFrame
{
  Q_OBJECT

  public:
    VIgraph(QWidget *parent = 0, Qt::WindowFlags f = 0);
    int heightForWidth (int w) const;   // Virtual function.
};
VIgraph.cpp
-----------------
#include <QFrame>
#include <VIgraph.h>

VIgraph::VIgraph(QWidget *parent, Qt::WindowFlags f):QFrame(parent,f)
{
  setFrameStyle(QFrame::Box|QFrame::Plain);
  // Aspect ratio
  QSizePolicy policy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  policy.setHeightForWidth(true);
  setSizePolicy(policy);
}
int VIgraph::heightForWidth(int w) const
{
  //if(height() > w)
    return w;
  //else
    //return height();
}
My main window object!!!
VIwindow.h
-----------------
#include <QMainWindow>
class VIgraph;
class VIwindow : public QMainWindow
{
  Q_OBJECT
  public:
    VIwindow();
  private:
    VIgraph     *m_graph;
};
VIwindow.cpp
-------------------
#include <QtGui>
#include "VIwindow.h"
#include "VIgraph.h"            // VI graphs objects.
VIwindow::VIwindow():QMainWindow()
{
  m_graph = new VIgraph();
  QVBoxLayout *graphLayout = new QVBoxLayout;
  graphLayout->addWidget(m_graph);
  graphLayout->addStretch();
  QHBoxLayout *firstRowLayout = new QHBoxLayout;
  firstRowLayout->addLayout(graphLayout);
  firstRowLayout->addStretch();

  QWidget *dummy = new QWidget();
  dummy->setLayout(firstRowLayout);
  setCentralWidget(dummy);
}
main.cpp
--------------
#include "QApplication"
#include "VIwindow.h"
int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  VIwindow *win = new VIwindow();
  win->show();
  return app.exec();
}
run.pro
----------
TEMPLATE      = app
HEADERS       = VIwindow.h \
                VIgraph.h
SOURCES       = main.cpp \
                VIwindow.cpp \
                VIgraph.cpp



On Thursday 15 July 2010 10:55, phil prentice wrote:
> Sorry Sean
>   Finger/Brain trouble...
>
>   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
> 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



More information about the Qt-interest-old mailing list