Can't position the QGraphicsView with fixed ratio

64 views Asked by At

My problem is about forcing QGraphicsView to save its ratio size. I read Keeping the aspect ratio of a sub-classed QWidget during resize and it helped me to position QWidget in example.

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);

  QWidget* wid = new QWidget;
  wid->setStyleSheet("background-color: red");

  QWidget* wid2 = new QWidget(wid);
  QVBoxLayout *vbl = new QVBoxLayout(wid);

  wid2->setStyleSheet("background-color: yellow");
  AspectRatioWidget w(wid2, 4, 3);
  w.setStyleSheet("background-color: blue");

  vbl->addWidget(&w);

  wid->show();

  return a.exec();
}

class AspectRatioWidget:

// header
class AspectRatioWidget : public QWidget
{
public:
  AspectRatioWidget(QWidget *widget, float width, float height, QWidget     *parent = 0);
  void resizeEvent(QResizeEvent *event);

private:
  QBoxLayout *layout;
  float arWidth; // aspect ratio width
  float arHeight; // aspect ratio height
};

// cpp
 AspectRatioWidget::AspectRatioWidget(QWidget *widget, float width, float       height, QWidget *parent) :
QWidget(parent), arWidth(width), arHeight(height)
{
  layout = new QBoxLayout(QBoxLayout::LeftToRight, this);

  // add spacer, then your widget, then spacer
  layout->addItem(new QSpacerItem(0, 0));
  layout->addWidget(widget);
  layout->addItem(new QSpacerItem(0, 0));
}

void AspectRatioWidget::resizeEvent(QResizeEvent *event)
{
  float thisAspectRatio = (float)event->size().width() / event->size().height();
  int widgetStretch, outerStretch;

  if (thisAspectRatio > (arWidth/arHeight)) // too wide
  {
    layout->setDirection(QBoxLayout::LeftToRight);
    widgetStretch = height() * (arWidth/arHeight); // i.e., my width
    outerStretch = (width() - widgetStretch) / 2 + 0.5;
  }
  else // too tall
  {
    layout->setDirection(QBoxLayout::TopToBottom);
    widgetStretch = width() * (arHeight/arWidth); // i.e., my height
    outerStretch = (height() - widgetStretch) / 2 + 0.5;
  }

  layout->setStretch(0, outerStretch);
  layout->setStretch(1, widgetStretch);
  layout->setStretch(2, outerStretch);
}

Situation when height is too big

Normal ratio

But when I tried to implement it with my elements, AspectRatioWidget was invisible.

//main:
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  wboard w;
  w.show();

  return a.exec();
}

//and constructor:

wboard::wboard(QWidget *parent) :
QWidget(parent)
{
  QWidget* wid= new QWidget(this);
  wid->setStyleSheet("background-color: red");

  QVBoxLayout *vbl = new QVBoxLayout(this);
  vbl->addWidget(wid);

  QWidget *window = new QWidget(wid);
  window->setStyleSheet("background-color: yellow");
  QVBoxLayout* vbl2 = new QVBoxLayout(wid);

  AspectRatioWidget w(window, 3, 1);
  w.setStyleSheet("background-color: blue");

  vbl2->addWidget(&w);
}

No blue and yellow widgets, just only red.

Code is very simular with upper, but idk.

1

There are 1 answers

0
MrTremolo On

Ok, problem was with constructing AspectRatioWidget without operator new. In first example AspectRatioWidget persisted on whole cycle of program events. In the second example that object deleted after the end of constructor of wboard.