[SOLVED] Get Focus rectangle Pen style and color



  • Hi guys!

    I almost finished making my first custom model/view. Now, I am trying to overcome the last obstacle : ). I have implemented my own delegates for each column of my TableView. Each delegate is painted by my reimplemented delegate paint function. The problem is that in this case u have to write all regular painting routine, like highlight item with highlight color, when it is in selected and active state, highlight it with button color, when it is in selected but not active state etc... I have succsessefully finished all that exсept one last thing - Focus rectangle. Do you have any ideas how to obtain pen for drawing focus rectangle? Ofcourse I can use any pen, but I want to have my focus rectangle look exactly like standard one.


  • Qt Champions 2016

    @Harb
    hi
    something like
    painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);

    wont work ?



  • @mrjj said:

    painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);

    well... no, because QPainter doesn't have such function :) QStyle has, but I don't know how to get pointer to QStyle subclass object which is used in my application.


  • Qt Champions 2016

    @Harb said:

    QStyle

    Oh so App has a custom Qstyle ?
    what does style() return?
    I used
    style()->drawControl(QStyle::CE_PushButton, &opt, &painter);
    but not sure with a custom style it works.
    Maybe
    QStyle* pStyle = QApplication::style();
    will give the correct one?



  • Well, I have solved the problem. Ended up, just copypasting Qt sourcecod.
    Here is that code:

        if (vopt->state & QStyle::State_HasFocus) {
                    QStyleOptionFocusRect o;
                    o.QStyleOption::operator=(*vopt);
                    o.rect = proxy()->subElementRect(SE_ItemViewItemFocusRect, vopt, widget);
                    o.state |= QStyle::State_KeyboardFocusChange;
                    o.state |= QStyle::State_Item;
                    QPalette::ColorGroup cg = (vopt->state & QStyle::State_Enabled)
                                  ? QPalette::Normal : QPalette::Disabled;
                    o.backgroundColor = vopt->palette.color(cg, (vopt->state & QStyle::State_Selected)
                                                 ? QPalette::Highlight : QPalette::Window);
                    proxy()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, p, widget);
                } 
    

  • Qt Champions 2016

    @Harb
    Cool. So PE_FrameFocusRect was useful.

    What is proxy() ?



  • @mrjj
    proxy() is a method of QStyle which return some sort of "proxy style". This code is from qcommonstyle.cpp file, and QCommonStyle evidently has this "proxy style". But in my case I just replaced proxy() with QApplication::style(), and everything works properly.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.