gpt4 book ai didi

c++ - Qt调整窗口大小以适应滚动区域问题中的纵横比

转载 作者:太空狗 更新时间:2023-10-29 23:06:43 27 4
gpt4 key购买 nike

我使用 Qt 制作了一个预览对象,它是一个容器,里面有屏幕小部件。为了使其宽高比正确,当屏幕的宽度改变时,高度也需要改变。

这会导致问题,因为屏幕在滚动区域内,当它的高度超过某个点时,滚动条就会出现。这会减小宽度,因此需要减小高度,这可能会导致循环滚动条不断进出。

我尝试解决此问题的一种方法是获取所需的大小,并在负责计算和设置大小的函数的多次迭代中取平均值,如果平均值与所需的大小没有显着差异,则大小不会改变。这在某种程度上解决了问题,但在我看来并不是最佳选择......

我想知道是否有人知道如何解决这个问题?

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

最佳答案

这行得通吗?

int minimum_size;

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

关于c++ - Qt调整窗口大小以适应滚动区域问题中的纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14816019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com