子类QCommonStyle 并像这样重新实现QCommonStyle::drawComplexControl:
if (const StyleOptionDecoratedGroupBox *groupBox = qstyleoption_cast<const StyleOptionDecoratedGroupBox *>(opt)) {
QRect textRect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, widget);
QRect checkBoxRect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxCheckBox, widget);
int decorationOffset = 0;
int pixmapRectWidth = 0;
int pixmapHeight = 0;
int textOffset = 0;
if (!groupBox->pixmap.isNull()) {
decorationOffset = groupBox->offset;
pixmapRectWidth = groupBox->leftSpace
+ groupBox->pixmap.width()
+ groupBox->rightSpace;
pixmapHeight = groupBox->pixmap.height();
textOffset = decorationOffset + pixmapRectWidth;
}
textRect.adjust(textOffset, 0, textOffset, 0);
// Draw frame
if (groupBox->subControls & QStyle::SC_GroupBoxFrame) {
QStyleOptionFrame frame;
frame.QStyleOption::operator=(*groupBox);
frame.features = groupBox->features;
frame.lineWidth = groupBox->lineWidth;
frame.midLineWidth = groupBox->midLineWidth;
frame.rect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxFrame, widget);
p->save();
QRegion region(groupBox->rect);
if (!groupBox->text.isEmpty()) {
bool ltr = groupBox->direction == Qt::LeftToRight;
QRect finalRect;
if (groupBox->subControls & QStyle::SC_GroupBoxCheckBox) {
finalRect = checkBoxRect.united(textRect);
finalRect.adjust(ltr ? -4 : 0, 0, ltr ? 0 : 4, 0);
} else {
finalRect = textRect;
}
region -= finalRect;
}
p->setClipRegion(region);
proxy()->drawPrimitive(PE_FrameGroupBox, &frame, p, widget);
p->restore();
}
// Draw icon
if (!groupBox->pixmap.isNull()) {
p->fillRect(decorationOffset, 0, pixmapRectWidth, pixmapHeight, opt->palette.window().color());
proxy()->drawItemPixmap(p, QRect(decorationOffset, 0, pixmapRectWidth, pixmapHeight),
Qt::AlignCenter, groupBox->pixmap);
}
// Draw title
if ((groupBox->subControls & QStyle::SC_GroupBoxLabel) && !groupBox->text.isEmpty()) {
QColor textColor = groupBox->textColor;
if (textColor.isValid())
p->setPen(textColor);
int alignment = int(groupBox->textAlignment);
if (!proxy()->styleHint(QStyle::SH_UnderlineShortcut, opt, widget))
alignment |= Qt::TextHideMnemonic;
proxy()->drawItemText(p, textRect, Qt::TextShowMnemonic | Qt::AlignHCenter | alignment,
groupBox->palette, groupBox->state & State_Enabled, groupBox->text,
textColor.isValid() ? QPalette::NoRole : QPalette::WindowText);
if (groupBox->state & State_HasFocus) {
QStyleOptionFocusRect fropt;
fropt.QStyleOption::operator=(*groupBox);
fropt.rect = textRect;
proxy()->drawPrimitive(PE_FrameFocusRect, &fropt, p, widget);
}
}
} else {
QCommonStyle::drawComplexControl(cc, opt, p, widget);
}