QGroupBox标题垂直对齐 [英] QGroupBox title vertical alignment
问题描述
我的UI中有QGroupBox。基本样式是2px宽度的边框,边框半径和标题垂直居中。
I have QGroupBox in my UI. The basic style is 2px width border, border radius and the title being vertically centered.
我在样式表中使用了以下样式(在.qrc中,应用于主要使用app-> setStylesheet):
I used the following style to my stylesheet (which is in a .qrc, applied in the main using app->setStylesheet):
QGroupBox {
border: 1px solid #22a4bc;
border-radius: 0px;
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top; /* position at the top center */
}
问题是标题是现在下降了几个像素,实际上在组框内的元素上方。
The problem is, the title is now a few pixel down, and actually over the element IN the groupbox.
我想将其居中。我尝试了垂直对齐,subcontrol-align,subcontrol-alignment甚至top:-5px,它实际上使标题居中,但修剪了高于边框的文本。
我在SO或Qt论坛上都找不到解决我问题的答案。
I want to set it centered. I tried vertical-align, subcontrol-align, subcontrol-alignment, even top: -5px which actually centers the title, but trim the text that is higher than the border. I didn't find any answer here on SO or on the Qt forum that resolve my problem.
有人知道如何将标题的垂直对齐设置为后中心?
(我使用C ++,Qt 5.2.1 / msvc2012,Qt Creator 3.6.1 / Windows 7)
Does anybody know how to set the title's vertical alignment to back center? (I use C++, Qt 5.2.1 / msvc2012, Qt Creator 3.6.1 / Windows 7)
推荐答案
我理解了我的错误:
根据框模型(边距>边框>填充>内容),我的文本的原点在边距中。但是我的QGroupBox中没有保证金,所以很奇怪。
I understood my mistake: According to the box model (margin > border > padding > content), the origin of my text was in the margin. But there were no margin in my QGroupBox, so it was weird.
我想出了我想要的这种样式:
I came up with this style which does what I want:
QGroupBox {
border: 1px solid #22a4bc;
border-radius: 0px;
padding-top: 10px;
margin-top: 5px;
}
QGroupBox:title {
subcontrol-origin: margin;
subcontrol-position: top center;
margin-left: 3px;
margin-right: 3px;
}
这篇关于QGroupBox标题垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!