[Qt-interest] How can I set text with icons in a QGroupBox?
Malyushytsky, Alex
alex at wai.com
Fri Nov 6 02:03:36 CET 2009
>> Do you know where I can find example code showing me how to do that?
Personally I like use QT own code if I need to learn something related in QT.
QT calls paint.drawComplexControl in the paintEvent.
drawComplexControl depends on the QStyle,
so you might want to look at the specific style drawComplexControl.
For example for common style qcommonstyle.cpp search for "drawComplexControl", then for "case CC_GroupBox:"
It will show you how to draw QCombobox.
Drawing icon is not that much different from drawing checkbox, which code below already does.
Just do additional adjustments to count your icon, similar to the adjustments for checkbox:
#ifndef QT_NO_GROUPBOX
case CC_GroupBox:
if (const QStyleOptionGroupBox *groupBox = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
// Draw frame
QRect textRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, widget);
QRect checkBoxRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxCheckBox, widget);
if (groupBox->subControls & QStyle::SC_GroupBoxFrame) {
QStyleOptionFrameV2 frame;
frame.QStyleOption::operator=(*groupBox);
frame.features = groupBox->features;
frame.lineWidth = groupBox->lineWidth;
frame.midLineWidth = groupBox->midLineWidth;
frame.rect = 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);
drawPrimitive(PE_FrameGroupBox, &frame, p, widget);
p->restore();
}
// 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 (!styleHint(QStyle::SH_UnderlineShortcut, opt, widget))
alignment |= Qt::TextHideMnemonic;
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;
drawPrimitive(PE_FrameFocusRect, &fropt, p, widget);
}
}
// Draw checkbox
if (groupBox->subControls & SC_GroupBoxCheckBox) {
QStyleOptionButton box;
box.QStyleOption::operator=(*groupBox);
box.rect = checkBoxRect;
drawPrimitive(PE_IndicatorCheckBox, &box, p, widget);
}
}
break;
#endif // QT_NO_GROUPBOX
-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Pedro Santos
Sent: Thursday, November 05, 2009 2:14 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] How can I set text with icons in a QGroupBox?
Am 05.11.2009 03:49, schrieb Malyushytsky, Alex:
> Then override virtual void paintEvent(QPaintEvent *);
> And draw QIcon the way you like.
> It might be not that easy to define suitable rules though.
> Remember that QGroupBox might display checkbox.
Do you know where I can find example code showing me how to do that?
Regards
Pedro Santos
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.
"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."
"Please consider our environment before printing this email."
More information about the Qt-interest-old
mailing list