tags:

views:

300

answers:

1

I'm trying to create custom widget inheriting QFrame. All works fine, but I'm unable to draw the focus rectangle around my widget. Below is the sample code I use for drawing:

frame.h

class Frame : public QFrame {
Q_OBJECT
public:
    Frame(QWidget *parent = 0);
    ~Frame();

protected:
    void paintEvent(QPaintEvent *event);

private:
    Ui::Frame *ui;
};

frame.cpp

Frame::Frame(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::Frame)
{
    ui->setupUi(this);
    setFocusPolicy(Qt::ClickFocus);
}

Frame::~Frame()
{
    delete ui;
}

void Frame::paintEvent(QPaintEvent *event)
{
    QFrame::paintEvent(event);
    if (hasFocus()) {
        QStylePainter painter(this);
        QStyleOptionFocusRect option;
        option.initFrom(this);
        option.backgroundColor = palette().dark().color();
        painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);
    }
}

What I mean by 'unable to draw focus frame' is that when you click a standard widget that accepts focus (let's say QLineEdit), it has a blue rectangle drawn around it. When I click my widget there is no such rectangle drawn. Are there any more things I should do besides setting focusPolicy on my widget?

+2  A: 

It might have something to do with the style your app is using. When I try your code with the "gtk" and "cleanlooks" style, no focus rectangle is drawn. With "plastique" and "windows" it is. Since I'm on Linux, I cannot test "windowsxp" and "macintosh". Try running with the -style option and see what happens.

andref
You are right. I code under kde4 so my default style is "oxygen" and it so happens that there is focus indicated. But it works perfectly fine with other styles. Thanks for the clue.
zarzych