void MyStyle::drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const { // check if the control element is a button if (element == QStyle::CE_PushButton) { // cast the style option to a button option const QStyleOptionButton *button = qstyleoption_cast(opt); // set the button background color p->fillRect(button->rect, Qt::red); // draw the button border QStyle::drawControl(element, button, p, w); } else { QStyle::drawControl(element, opt, p, w); } }
void MyStyle::drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const { if (element == QStyle::CE_ProgressBar) { const QStyleOptionProgressBar *progressBar = qstyleoption_castIn this example, we are customizing the appearance of the progress bar control in our application. We check if the control element is a progress bar and then set the background color of the progress bar to grey. We draw the progress bar value using a green color and adjusting the rectangle width. We also call the default QStyle implementation for other control elements that are not progress bars. The package library for the QStyle class is the QtGui module of the Qt framework.(opt); // set the progress bar alignment p->setBrush(Qt::gray); p->setPen(Qt::NoPen); p->drawRect(progressBar->rect); // draw the progress bar value QRect rect = progressBar->rect; rect.setWidth((rect.width() * progressBar->progress) / progressBar->maximum); p->setBrush(Qt::green); p->setPen(Qt::NoPen); p->drawRect(rect); } else { QStyle::drawControl(element, opt, p, w); } }