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. Customise border radius of QTableView row selection
QtWS25 Last Chance

Customise border radius of QTableView row selection

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtableview
7 Posts 4 Posters 1.6k 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.
  • S Offline
    S Offline
    siya
    wrote on 14 Sept 2021, 17:25 last edited by
    #1

    I would like to customise the QTableView selection border, ie when I select a row in the table, the selection border should be a circular (the border-radius function).

    R 1 Reply Last reply 14 Sept 2021, 18:10
    0
    • S siya
      14 Sept 2021, 17:25

      I would like to customise the QTableView selection border, ie when I select a row in the table, the selection border should be a circular (the border-radius function).

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 14 Sept 2021, 18:10 last edited by
      #2

      @siya
      subclassing QStyleItemDelegate should do what you want (untested though):

      void MyStyleItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,  const QModelIndex &index) const
         {
                 bool isSelected = option.state & QStyle::State_Selected;
                 QStyleOptionViewItem opt = option;
                 opt.state &= ~QStyle::QStyle::State_Selected;
                 QStyledItemDelegate::paint(painter, opt, index);
                 if( isSelected ){
                      // draw rounded rect using painter
                 }
         }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      J 1 Reply Last reply 14 Sept 2021, 21:14
      1
      • R raven-worx
        14 Sept 2021, 18:10

        @siya
        subclassing QStyleItemDelegate should do what you want (untested though):

        void MyStyleItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,  const QModelIndex &index) const
           {
                   bool isSelected = option.state & QStyle::State_Selected;
                   QStyleOptionViewItem opt = option;
                   opt.state &= ~QStyle::QStyle::State_Selected;
                   QStyledItemDelegate::paint(painter, opt, index);
                   if( isSelected ){
                        // draw rounded rect using painter
                   }
           }
        
        J Offline
        J Offline
        JoeCFD
        wrote on 14 Sept 2021, 21:14 last edited by JoeCFD
        #3

        @raven-worx He wants to do this for the whole row, not for one item. I think this may be the last thing for him to do in his project.

        R 1 Reply Last reply 15 Sept 2021, 09:03
        0
        • J JoeCFD
          14 Sept 2021, 21:14

          @raven-worx He wants to do this for the whole row, not for one item. I think this may be the last thing for him to do in his project.

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 15 Sept 2021, 09:03 last edited by
          #4

          @JoeCFD
          still possible (and the only way i think).
          It needs to be checked if its a middle index or an index on the side of the view and draw the borders accordingly

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          J 1 Reply Last reply 15 Sept 2021, 13:50
          1
          • R raven-worx
            15 Sept 2021, 09:03

            @JoeCFD
            still possible (and the only way i think).
            It needs to be checked if its a middle index or an index on the side of the view and draw the borders accordingly

            J Offline
            J Offline
            JoeCFD
            wrote on 15 Sept 2021, 13:50 last edited by
            #5

            @raven-worx Agree. That is the part missing in your previous code.

            S 1 Reply Last reply 16 Sept 2021, 08:32
            0
            • J JoeCFD
              15 Sept 2021, 13:50

              @raven-worx Agree. That is the part missing in your previous code.

              S Offline
              S Offline
              siya
              wrote on 16 Sept 2021, 08:32 last edited by
              #6

              @JoeCFD @raven-worx Yes that is what I would like. I suppose the onus is on me to read up on the paint function and how to paint borders depending on which index is selected?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 16 Sept 2021, 19:22 last edited by
                #7

                Hi,

                You have the model index, so if it's the left most one, draw the left round corners and if it's the right most one, draw the right rounded corner.

                The other option is to use one delegate for the first and last column to do the special painting. Note that this is not the correct solution if your users can reorder the columns.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                5/7

                15 Sept 2021, 13:50

                • Login

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