在Qt中,如何在不同的组框中对齐表单元素? [英] In Qt, how do I align form elements in different group boxes?

查看:144
本文介绍了在Qt中,如何在不同的组框中对齐表单元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个标准的两列表单,其中第一列是文本标签(QLabel),第二列是交互式窗口小部件,通常是文本字段(QLineEdit).我们已决定将共享相同主题的表单行放置在QGroupBox内,因此与主布局相比应放置在单独的布局中.因此,每个QGroupBox内部的表单元素都不会与QGroupBox es外部的表单元素水平对齐.

I'm trying to create a standard two-column form, where the first column is a text label (QLabel) and the second column is an interactive widget, typically a text field (QLineEdit). We have decided to place form rows that share a common theme inside a QGroupBox, and thus in a separate layout than the main layout. Thus, the form elements inside each QGroupBox do not horizontally align with the form elements outside of the QGroupBoxes.

如何使用组框和布局,使内部框和外部组框内的QLabelQLineEdit(水平)彼此对齐?

How can I use group boxes and layouts in a way such that the QLabels and QLineEdits both inside and outside group boxes are (horizontally) aligned with each other?

谢谢!

推荐答案

您可以将组框内所有标签的minimumWidth属性设置为足以显示最宽标签的宽度.这样可以使不同组框中的所有标签对齐.

You can set the minimumWidth property on all of the labels inside the groupboxes to something that is just wide enough to display the widest label. That will get all of the labels inside the different group boxes aligned.

现在,要使分组框外部的标签与内部分组框对齐:首先,我假设每个标签/行编辑对都具有自己的水平布局,或者您在网格内有多行.无论哪种情况,您都可以将标签的minimumWidth设置为与组框中的标签相同的值.最后,调整水平(或网格)布局上的layoutLeftMargin,layoutRightMargin和layoutSpacing属性,直到label/lineedit对的左右边缘与组框内的左右边缘对齐.

Now, to get the labels outside of the groupboxes aligned with those inside: First, I assume that each label/lineedit pair is in its own horizontal layout, or that you have multiple rows inside of a grid. In either case, what you can do is set the minimumWidth of the labels to the same value as the labels in the groupboxes. Finally, adjust the layoutLeftMargin, layoutRightMargin, and layoutSpacing properties on the horizontal (or grid) layout until the right and left edges of the label/lineedit pair align with those inside the groupboxes.

如果您尚未在Qt Creator或Qt Designer中使用表单编辑器来构建UI,我发现它使此任务变得相当简单.

If you're not already using the Form Editor in Qt Creator, or Qt Designer, to build your UI, I found it to make this task fairly easy.

我必须承认,这有点不合时宜,但是在我构建的简单测试用例中,它似乎可以正常工作.另一方面,如果用户更改字体大小,这似乎可能会中断.也许有更好的方法吗?

I have to admit, this feels a little kludgey, but in the simple test case I built, it seemed to work okay. On the other hand, this seems likely to break if the user changes the font size. Maybe there's a better way?

希望这会有所帮助.

这篇关于在Qt中,如何在不同的组框中对齐表单元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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