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
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Get Focus rectangle Pen style and color

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.0k Views 2 Watching
  • 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.
  • H Offline
    H Offline
    Harb
    wrote on 5 Sept 2015, 20:39 last edited by Harb 9 Jul 2015, 11:44
    #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.

    M 1 Reply Last reply 5 Sept 2015, 20:45
    0
    • H Harb
      5 Sept 2015, 20:39

      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.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Sept 2015, 20:45 last edited by
      #2

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

      wont work ?

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Harb
        wrote on 5 Sept 2015, 21:02 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
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 5 Sept 2015, 21:08 last edited by mrjj 9 May 2015, 21:13
          #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
          • H Offline
            H Offline
            Harb
            wrote on 7 Sept 2015, 11:43 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);
                        } 
            
            M 1 Reply Last reply 7 Sept 2015, 11:56
            1
            • H Harb
              7 Sept 2015, 11:43

              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);
                          } 
              
              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 7 Sept 2015, 11:56 last edited by
              #6

              @Harb
              Cool. So PE_FrameFocusRect was useful.

              What is proxy() ?

              H 1 Reply Last reply 7 Sept 2015, 12:29
              0
              • M mrjj
                7 Sept 2015, 11:56

                @Harb
                Cool. So PE_FrameFocusRect was useful.

                What is proxy() ?

                H Offline
                H Offline
                Harb
                wrote on 7 Sept 2015, 12:29 last edited by Harb 9 Jul 2015, 12:30
                #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

                6/7

                7 Sept 2015, 11:56

                • Login

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