JimDaniel
25th January 2008, 15: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...
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...