PDA

View Full Version : Need some help re-painting a checkbox



JimDaniel
25th January 2008, 16:35
Hi, I'd like to replace the default checkbox with images of my own, but I'm having some difficulties. Just doesn't work, as if nothing is happening. Here is the code where I try to "catch" the checkbox and redraw, keep in mind I don't really know what I'm doing, just feeling my way:



void ResoStyle::drawControl(ControlElement control, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
switch (control)
{
//replaces default CheckBox style with our own
case CE_CheckBox:
if(const QStyleOptionButton * checkOption = qstyleoption_cast<const QStyleOptionButton *>(option))
{
const QCheckBox * checkbox = qobject_cast<const QCheckBox *>(widget);

QStyleOptionButton newCheckBox(*checkOption);

QRect frame = newCheckBox.rect;


painter->save();
painter->setBrushOrigin(frame.topLeft());

if(newCheckBox.state & State_MouseOver)
{
if(!checkbox->isChecked())
{
painter->drawImage(frame, QImage("./Images/disabled_hover.png"));
}
else
{
//draw the color of the current module class
painter->drawImage(frame, QImage("./Images/personal_enabled.png"));
}
}
else
{
if(!checkbox->isChecked())
{
painter->drawImage(frame, QImage("./Images/disabled_default.png"));
}
else
{
//draw the color of the current module class
painter->drawImage(frame, QImage("./Images/personal_enabled.png"));
}
}
painter->restore();
QCleanlooksStyle::drawControl(control, &newCheckBox, painter, widget);
}
else
{
QCleanlooksStyle::drawControl(control, option, painter, widget);
}
break;


One thing I'd also like to understand is, is this the best way to do something like this. Would I also be able to just override the paintEvent for the widget and redraw there just as well?

Thanks for any help you can give...

jpn
25th January 2008, 17:02
Are style sheets out of question..? I believe it would be pretty much straightforward to do with "pseudo states (http://doc.trolltech.com/latest/stylesheet-syntax.html#pseudo-states)".

JimDaniel
25th January 2008, 17:08
Are style sheets out of question..? I believe it would be pretty much straightforward to do with "pseudo states (http://doc.trolltech.com/latest/stylesheet-syntax.html#pseudo-states)".

Yeah, I've thought about using style sheets, I may play with them and see what I can get. I was hesitant about shoe-horning them into my code though, as the rest of my code uses this QStyle. Thanks for the tip!