Qt:对齐在单独的布局中的控件 [英] Qt: Align controls that are in separate layouts

查看:647
本文介绍了Qt:对齐在单独的布局中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Qt Designer设计的表单上,我有两个QGroupBoxes,每个控件中都有一堆控件.这两个组框的内容几乎相同(带有关联标签的QLineEdits).

On a form designed with Qt Designer, I have two QGroupBoxes with a bunch of controls in each of them. Both group boxes have nearly the same contents (QLineEdits with associated labels).

但是,我要做的是将控件对齐在一起,就像它们是同一网格布局的一部分一样.但是,由于它们位于单独的容器中,因此它们不能共享相同的布局,并且我不想给它们提供固定的宽度.

What I want to do, however, is to align the controls together, as if they were part of the same grid layout. But since they are in separate containers, they can't share the same layout, and I don't want to give them a fixed width.

在Qt Designer中有没有办法做到这一点?如果没有,有没有办法在代码中做到这一点?

Is there a way to do it in Qt Designer? If not, is there a way to do it in code?

谢谢!

推荐答案

在Designer中无法执行此操作.据我所知,Qt也不提供在代码中执行此操作的好方法.如果您确实要这样做,则可能不得不依靠一些小技巧.

There is no way to do this in Designer. As far as I know, Qt does not provide a good way to do this in code either. If you really want this, you will probably have to rely on something a little hacky.

这是我的第一个想法:覆盖包含两个组框的小部件中的resizeEvent(),以获取所有标签的首选大小(通过sizeHint()或minimumSizeHint())并设置所有标签的固定宽度到最大的首选宽度.

Here's my first idea: Override resizeEvent() in the widget that contains the two group boxes to get the preferred size (via sizeHint() or minimumSizeHint()) of all of the labels and set the fixed width of all the labels to the largest preferred width.

我鼓励您问自己这是否真的很重要(是否值得开发时间?),并考虑使用稍微不同的UI设计是否可以完全避免该问题.

I would encourage you to ask yourself if this really matters (is it worth the development time?) and consider whether you can avoid the problem entirely with a slightly different UI design.

顺便说一句,如果您还没有的话,您可能想看看QFormLayout.

BTW, you might want to take a look at QFormLayout if you haven't already.

这篇关于Qt:对齐在单独的布局中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆