简体   繁体   English

限制QWidget的大小

[英]Limit QWidget size

Let's say I have an wall where user can have only N windows. 假设我有一堵墙,用户只能有N窗口。 I should restrict the user from setting one window over another. 我应该限制用户将一个窗口设置为另一个窗口。 He can move, resize them but he cannot overlap them. 他可以移动并调整它们的大小,但不能重叠它们。 To ignore the invalid moves I am catching the moveEvents and if the event->pos() is an invalid position I reset the geometry to the event->oldPos() . 为了忽略无效移动,我捕获了moveEvents ,如果moveEvents event->pos()是无效位置,则将几何重置为event->oldPos() Well this approach is not working for resizeEvent . 那么这种方法不适用于resizeEvent
Why I can't use same approach on resize? 为什么我不能在调整大小时使用相同的方法?
How can I solve this problem? 我怎么解决这个问题?

PS Setting maximumSize and minimumSize is not a good idea, it's to consuming because of my implementation. PS设置maximumSize和minimumSize不是一个好主意,因为我的实现很费力。

When a user resized a window or resize is called Qt sets Qt::WA_WState_ConfigPending flag to true and starts internal resize operations. 当用户调整窗口resizeresize Qt::WA_WState_ConfigPending标志设置为true并开始内部调整大小操作。 During these operations you receive resizeEvent . 在这些操作中,您会收到resizeEvent If you try to call resize there Qt just checks that the flag is set and ignores new resize until previous one is finished, however it sends resizeEvent again with the same data and you get into an endless recursion. 如果您尝试在此处调用resizeQt只会检查该标志是否已设置,并忽略新的调整大小,直到上一个调整完成为止,但是resizeEvent再次使用相同的数据发送resizeEvent ,您将陷入无限循环。

You can use this trick to overcome the check: 您可以使用此技巧来克服检查:

void widget::resizeEvent(QResizeEvent* e)
{
    if (e->size().width() > 800)
    {
        setAttribute(Qt::WA_WState_ConfigPending, false);
        resize(e->oldSize());
    }
}

However, you still get blinking of the window, because it's already resized when you get resizeEvent . 但是,仍然会闪烁窗口,因为在获取resizeEvent时已经调整了窗口的大小。 Actually OS resizes a window and then sends WM_SIZE message to a window to handle the change, that's why you have to deal with the result of the resize operation. 实际上,操作系统会调整窗口大小,然后将WM_SIZE消息发送到窗口以处理更改,这就是为什么您必须处理调整大小结果的原因。

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

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