Qt Resize window to fit aspect ratio in scroll area

I have created a preview object using Qt, which is a container that has a screen widget in it. To make its aspect ratio correct, when the screen width changes, the height must also change.

This causes a problem as the screen is inside the scroll area when its height is outside a certain point that the scroll bar enters. This decreases the width and so the height must be reduced, this can cause a loop where the scrollbar keeps coming in and out.

One way I tried to fix it is to accept the required size and average over multiple iterations of the function that is responsible for designing and sizing if the average is not significantly different from the required size. Will not change. This has a definite problem, but seems less optimal to me ...

I am wondering if anyone has any idea how to fix this problem?

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

      

+3


source to share


1 answer


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;

      

+1


source







All Articles