Personal tools
You are here: Home Developer Resources Knowledgebase How can I avoid drawing the focus rect on my buttons?
Document Actions

How can I avoid drawing the focus rect on my buttons?



Answer:

In order to not draw the focus rect of the buttons it is necessary to subclass the style and reimplement drawControl() to strip out the State_HasFocus flag for the buttons.

See the documentation:

http://doc.trolltech.com/4.3/qstyle.html#drawControl

See the following example:

#include <QApplication>
#include <QtGui>

class Style : public QWindowsStyle
{
public:
Style()
{
}

void drawControl ( ControlElement element, const QStyleOption * option,
QPainter * painter, const QWidget * widget = 0 ) const
{
if(element == CE_PushButton) {
const QStyleOptionButton *b = qstyleoption_cast<const QStyleOptionButton *>(option);
QStyleOptionButton *btn = (QStyleOptionButton *)b;
if (btn) {
if (btn->state & State_HasFocus) {
btn->state = btn->state ^ State_HasFocus;
}
}
QWindowsStyle::drawControl(element, btn, painter, widget);

} else {
QWindowsStyle::drawControl(element, option, painter, widget);
}
}

};
int main(int argc, char **argv)
{
QApplication app(argc, argv);

QWidget box;
QPushButton *button1 = new QPushButton(&box);
button1->setText("Number one");

QPushButton *button2 = new QPushButton(&box);
button2->setText("Number two");

button1->setStyle(new Style());
button2->setStyle(new Style());

QVBoxLayout *layout = new QVBoxLayout(&box);
layout->addWidget(button1);
layout->addWidget(button2);

box.show();
return app.exec();
}
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: