QGroupBox title vertical alignment

本小妞迷上赌 提交于 2020-01-23 18:00:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!