Qt:对齐单独布局中的控件

发布于 2024-09-26 01:55:41 字数 250 浏览 1 评论 0原文

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

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

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

谢谢!

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.

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不打扰别人 2024-10-03 01:55:41

Designer 中无法执行此操作。据我所知,Qt 也没有提供在代码中执行此操作的好方法。如果你真的想要这个,你可能不得不依赖一些有点hacky的东西。

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

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

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

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.

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文