【问题标题】:Can't position the QGraphicsView with fixed ratio无法以固定比例定位 QGraphicsView
【发布时间】:2017-09-08 08:30:06
【问题描述】:

我的问题是关于强制 QGraphicsView 保存其比例大小。我读了Keeping the aspect ratio of a sub-classed QWidget during resize 它帮助我在示例中定位 QWidget。

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();
}

类 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

但是当我尝试用我的元素实现它时,AspectRatioWidget 是不可见的。

//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);
}

没有蓝色和黄色小部件,只有红色。

代码和upper很相似,但是idk。

【问题讨论】:

    标签: c++ qt qwidget qlayout


    【解决方案1】:

    好的,问题在于在没有 operator new 的情况下构造 AspectRatioWidget。 在第一个示例中,AspectRatioWidget 在整个程序事件周期中持续存在。在第二个例子中,对象在 wboard 的构造函数结束后被删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多