PDA

View Full Version : Not drawing the rubberband around the focus item in QListWidget



Antrax
15th October 2006, 15:34
The title says it all, I don't like the rubber band (that stroked rectangle you get around an item after you click it), so I subclassed QCommonStyle to override it:

class MyListWidgetStyle : public QCommonStyle
{
public:
MyListWidgetStyle () : QCommonStyle() {}
~MyListWidgetStyle () {}

virtual void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = 0) const
{
switch(element)
{
case CE_FocusFrame: //surpressing these
case CE_RubberBand:
return;
default:
QApplication::style()->drawControl(element, option, painter, widget);
}
}
};
And then in MyListWidget constructor's I call setStyle(new MyListWidgetStyle()). The only problem is that it doesn't do anything. In fact, drawControl is never called. What could be the reason for that? The only thing I could think of is that the application style isn't calling drawControl at all (which seems far-fetched, but okay), but when I override the entire application's style with this, it still doesn't work.
Any ideas or suggestions?

jpn
15th October 2006, 18:53
Any ideas or suggestions?
Yes, it's extremely easy to disable drawing of the focus rect by delegates. All you have to do is to subclass QItemDelegate and override drawFocus() with an empty implementation.



class MyItemDelegate : public QItemDelegate
{
public:
MyItemDelegate(QObject* parent = 0) : QItemDelegate(parent) {}

protected:
void drawFocus(QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect) const {}
};

// usage
listWidget->setItemDelegate(new MyItemDelegate(listWidget));

Antrax
16th October 2006, 09:33
Oh wow, that WAS simple. Thanks. I guess I'll have to understand what those delegates are, I didn't read too much about it because I have no need for anything elaborate with the model/view pattern, I just wanted a list box :-)

jpn
7th November 2006, 09:35
If you don't need focus at all, you can simply do:


listWidget->setFocusPolicy(Qt::NoFocus);