Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Get Focus rectangle Pen style and color
QtWS25 Last Chance

[SOLVED] Get Focus rectangle Pen style and color

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • HarbH Offline
    HarbH Offline
    Harb
    wrote on last edited by Harb
    #1

    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.

    mrjjM 1 Reply Last reply
    0
    • HarbH Harb

      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.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      wont work ?

      1 Reply Last reply
      0
      • HarbH Offline
        HarbH Offline
        Harb
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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?

          1 Reply Last reply
          0
          • HarbH Offline
            HarbH Offline
            Harb
            wrote on last edited by
            #5

            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);
                        } 
            
            mrjjM 1 Reply Last reply
            1
            • HarbH Harb

              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);
                          } 
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Harb
              Cool. So PE_FrameFocusRect was useful.

              What is proxy() ?

              HarbH 1 Reply Last reply
              0
              • mrjjM mrjj

                @Harb
                Cool. So PE_FrameFocusRect was useful.

                What is proxy() ?

                HarbH Offline
                HarbH Offline
                Harb
                wrote on last edited by Harb
                #7

                @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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved