[Qt-interest] aspect-ratio for graphs

phil prentice philp.cheer at talktalk.net
Wed Jul 14 18:57:06 CEST 2010


Hi
  I am sorry to say that I think that this question has been asked a few times 
before, with varying answers.  I am trying to draw six graphs side by side 
with a number of widgets displayed below it.  I am using horizontal and 
vertical layouts to do this.  And yes I would like to force the aspect ratio 
of the graphs to be 1:1.  Reading various qt-interest forum answers it seemed 
to suggest that I could use the member functions setSizePolicy() and 
heightForWidth()...which I have tried to do with no success. 
Is the general principle of what I am trying to do correct? Any ideas as to 
why it does not work for me?

Any help would be welcome....I love QT but sometimes its just too hard!!!

Thanks
Phil

 I have defined a class called VIgraph() derived from QFrame() i.e.
VIgraph::VIgraph(QWidget *parent, Qt::WindowFlags f):QFrame(parent,f)
{
  // Create a frame around the graphs.
  setFrameStyle(QFrame::Box|QFrame::Plain);
  QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  policy.setHeightForWidth(true);  // Keep aspect ratio
  setSizePolicy(policy);
}

int VIgraph::heightForWidth(int w) const
{
  if(height() > w)
    return w;
  else
    return height();
}

My main window widget creates various widgets(including the VIgraph widgets) 
and lays them out (see two following functions)

void VIwindow::createWidgets()
{
  int i;

  // Create the 6 graphs, the 6 labeled Pass/Fail labels and the 6 checkboxes.
  for(i = 0; i < 6; i++)
  {
    m_graph[i] = new VIgraph();
    m_graphPassFailLabel[i] = new QLabel();
    m_graphPassFailTextLabel[i] = new QLabel(QString("VI%1Pass/Fail").arg(i));
    m_graphCheckBoxs[i] = new QCheckBox(QString("Select VI%1").arg(i));
  }
  // Create Tolerance slider and tolerance checkbox along with label.
  m_toleranceSlider = new QSlider(Qt::Horizontal);
  m_toleranceLineEdit = new QLineEdit();
  m_toleranceCheckBox = new QCheckBox("View Tolerance");
  // Create Save & Cancel pushbuttons.
  m_saveToleranceButton = new QPushButton(tr("Save Tol"));
  m_cancelToleranceButton = new QPushButton(tr("Cancel"));
  // Auto/Manual Combi-box.
  m_autoManualComboBox = new QComboBox();
  m_autoManualComboBox->addItem("Auto");
  m_autoManualComboBox->addItem("Manual");
 // Single-shot & Continuous pushbuttons.
  m_singleShotButton = new QPushButton(tr("Single Shot"));
  m_continuousButton = new QPushButton(tr("Continuous"));

  // Set fixed widgets attributes.
  m_toleranceLineEdit->setReadOnly(true);
  m_toleranceSlider->setMinimum(0);
  m_toleranceSlider->setMaximum(100);
  m_toleranceSlider->setTickInterval(1);

  QPixmap greyPixmap(":resources/images/greyButton.png"); // Default pixmap
  QSize max(16, 16);
  for(i = 0; i < 6; i++)
  {
    m_graphPassFailLabel[i]->setPixmap(greyPixmap);
    // Set Label sizes and scale the contents.
    m_graphPassFailLabel[i]->setMaximumSize(max);
    m_graphPassFailLabel[i]->setScaledContents(true);
  }
]

void VIwindow::doLayout()
{
  int i;

  // Create Pass/Fail label with text as horizontal layout.
  QHBoxLayout *passFailLayout[6];
  for(i = 0; i < 6; i++)
  {
    passFailLayout[i] = new QHBoxLayout;
    passFailLayout[i]->addWidget(m_graphPassFailLabel[i]);
    passFailLayout[i]->addWidget(m_graphPassFailTextLabel[i]);
    passFailLayout[i]->addStretch();
  }
  // Create the six vertical graph layouts with label.
  QVBoxLayout *graphLayout[6];
  for(i = 0; i < 6; i++)
  {
    graphLayout[i] = new QVBoxLayout;
    graphLayout[i]->addWidget(m_graph[i]);       // Aspect-ratio needed.
    graphLayout[i]->addLayout(passFailLayout[i]);
  }

  // Plonk all graph layouts into one horizontal layout.
  QHBoxLayout *firstRowLayout = new QHBoxLayout;
  for(i = 0; i < 6; i++)
  {
    firstRowLayout->addLayout(graphLayout[i]);
  }
  // Layout the checkbox row to follow.
  QHBoxLayout *secondRowLayout = new QHBoxLayout;
  for(i = 0; i < 6; i++)
  {
    secondRowLayout->addWidget(m_graphCheckBoxs[i]);
  }
  // Layout the tolerance row.
  QHBoxLayout *thirdRowLayout = new QHBoxLayout;
  thirdRowLayout->addWidget(m_toleranceSlider);
  thirdRowLayout->addWidget(m_toleranceLineEdit);
  thirdRowLayout->addWidget(m_toleranceCheckBox);
  thirdRowLayout->addWidget(m_saveToleranceButton);
  thirdRowLayout->addWidget(m_cancelToleranceButton);

  // Layout the auto/manual and single-shot buttons.
  QHBoxLayout *fourthRowLayout = new QHBoxLayout;
  fourthRowLayout->addStretch();
  fourthRowLayout->addWidget(m_autoManualComboBox);
  fourthRowLayout->addWidget(m_singleShotButton);
  fourthRowLayout->addWidget(m_continuousButton);

  // Do Top layout.
  QVBoxLayout *topLayout = new QVBoxLayout;
  topLayout->addLayout(firstRowLayout);
  topLayout->addLayout(secondRowLayout);
  topLayout->addLayout(thirdRowLayout);
  topLayout->addLayout(fourthRowLayout);

  QWidget *dummy = new QWidget();
  dummy->setLayout(topLayout);
  setCentralWidget(dummy);
}







More information about the Qt-interest-old mailing list