是否可以在QGroupBox标题中添加图标(以编程方式,通过QtCreator,带有或不带有样式表)?
我想在我的GUI中靠近某些QGroupBoxes的标题放置一个标记。最佳位置将在标题的右侧,但任何其他合理位置(在标题的左侧或小部件的右上角)也可能有效。
我能想到的唯一方法是创建带有图标的QLabel,然后将其可视地放置在QGroupBox顶部的所需位置。但是我发现这是一个丑陋的解决方案。
编辑
我知道有一种方法可以使用样式表将用于可检查QGroupBoxes的indicator
设置为任何图像,包括已检查和未检查状态的任何图像。但是,然后QGroupBox必须是可检查的,等等。我想将此标记(小图像)独立地添加到QGroupBox的可检查性中。
最佳答案
背景
不幸的是,QGroupBox
不像setIcon
那样具有QPushButton
方法。
可能的解决方案是使用 QGroupBox
使QGroupBox::setCheckable
可检查,并使用stylesheet将自定义图像设置为指示符,如下所示:
QGroupBox::indicator:unchecked {
image: url(:/images/checkbox_unchecked.png);
}
但是,如果取消选中该复选框,则会禁用整个组框。
解
考虑到这种背景,我建议您使用一个相当复杂但又非常灵活的解决方案,包括以下步骤:
QStyleOptionGroupBox
并声明其他变量作为公共(public)类属性:QPixmap pixmap;
int offset;
int leftSpace;
int rightSpace;
这些将用于将装饰像素图以及放置设置传递给样式。
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);
}
QGroupBox
和重新实现 QGroupBox::paintEvent
像这样:CustomStyle style;
QPainter painter(this);
StyleOptionDecoratedGroupBox option;
initStyleOption(&option);
option.pixmap = m_pixmap;
option.offset = m_offset;
option.leftSpace = m_leftSpace;
option.rightSpace = m_rightSpace;
style.drawComplexControl(QStyle::CC_GroupBox, &option, &painter, this);
注意:当可选中组框时,此解决方案不能很好地工作。
例
该解决方案需要大量工作,但是幸运的是,我已经为您创建了必要的类,以及如何在您自己的项目中使用它们的完整示例。该代码在GitHub上可用。
结果
提供的示例产生以下结果: