简体   繁体   English

Qt调整窗口大小以适应滚动区域问题中的宽高比

[英]Qt Resizing a window to fit an aspect ratio in a scroll area issue

I've made a preview object using Qt which is a container that has the screen widget inside it. 我使用Qt创建了一个预览对象,这是一个内部有屏幕小部件的容器。 In order to make its aspect ratio correct, when the width of the screen is changed the height also needs to be changed. 为了使其宽高比正确,当改变屏幕的宽度时,还需要改变高度。

This causes a problem as the screen is inside of a scroll area, when its height goes beyond a certain point the scroll bar comes in. This reduces the width and therefore the height needs to be reduced, this can cause a loop where the scroll bar keeps coming in and out. 这会导致问题,因为屏幕位于滚动区域内部,当其高度超过滚动条进入的某个点时。这会减小宽度,因此需要降低高度,这会导致滚动条出现滚动条不断进出。

One way I have tried to fix this is taking the required size and averaging over a number of iterations of the function that is responsible for working out and setting the size, if the average is not significantly different from the required size the size will not change. 我尝试解决这个问题的一种方法是在负责计算和设置大小的函数的多次迭代中采用所需的大小和平均值,如果平均值与所需大小没有显着差异,则大小不会改变。 This has sort of fixed the problem but seems to me less than optimal... 这有点解决了这个问题,但在我看来并不是最优的......

I wondering if anyone has an idea of how to fix this issue? 我想知道是否有人知道如何解决这个问题?

//********************************************************************************
/// \brief Called to make the size of the preview pane the correct size
void PreviewDisplayWidget::SetSizeOfScreen()
{
  double aspect_ratio = _preview_controls->_video_settings.getVideoFormat().GetAspectRatio();

  // Calculate the new minimum size
  int minimum_size = _container->width()/aspect_ratio;

  // HACK TO FIX ISSUE WITH RESIZING:
  // The preview sets its height from the width, if the width is changed, so will the height.
  // This causes an issue with the scroll area as it can be in a situation that will cause the
  // scroll area to get thinner (due to scroll bars being added), causing the width of the widget
  // to change, therefore causing the height to change. Which can be enough to make the scroll bar go
  // away. This can loop and cause a stack overflow.
  //
  // Store the last 10 sizes.
  _previous_sizes.push_back(minimum_size);

  const int sizes_to_scan = 10;
  if (_previous_sizes.size() > sizes_to_scan)
  {
    _previous_sizes.pop_front();
  }

  int sum = 0;
  for (auto i =_previous_sizes.begin(); i != _previous_sizes.end(); i++)
  {
    sum += *i;
  }

  double average = (double) sum / (double) _previous_sizes.size();

  std::cout << "Average " << average << std::endl;

  bool change_in_average = false;
  // Significant change in average means a size change should occur
  if (average > minimum_size + 5 || average < minimum_size - 5)
  {
    change_in_average = true;
  }


  if (change_in_average || _previous_sizes.size() < sizes_to_scan - 1)
  {
    _preview_frame->setMinimumHeight(minimum_size);
    std::cout << "Preview sizes " << minimum_size << std::endl;
    _preview_frame->updateGeometry();
  }

  SetPreviewSize();
}

Will this work? 这会有用吗?

int minimum_size;

if(scrollArea->verticalScrollBar())
minimum_size = (_container->width()+scrollArea->verticalScrollBar()->width())/aspect_ratio;
else
minimum_size = _container->width()/aspect_ratio;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM