问题
I have QGroupBox in my UI. The basic style is 2px width border, border radius and the title being vertically centered.
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.
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.
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)
回答1:
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;
}
来源:https://stackoverflow.com/questions/38079435/qgroupbox-title-vertical-alignment