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. QTableView / QItemDelegate customize selection
Qt 6.11 is out! See what's new in the release blog

QTableView / QItemDelegate customize selection

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.7k 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.
  • TobyT Offline
    TobyT Offline
    Toby
    wrote on last edited by
    #1

    Hi everyone

    because the user must be able to see the row's background-color i want to replace the default row-selection-style with a 1px frame at the rows/cells borders.
    That's why i subclassed QItemDelegate and implemented my own paint-Method.

    void QSelectionItemDelegate::paint( QPainter *painter_, const QStyleOptionViewItem &option_, const QModelIndex &index_ ) const
    {
        QStyleOptionViewItem opt = option_;
        this->QItemDelegate::paint( painter_, opt, index_ );
        if ( option_.state & QStyle::State_Selected )
        {
            painter_->setPen( Qt::red );
            painter_->drawRect( option_.rect );
        }
    }
    

    Drawing the red frame works perfectly, but the default row-selection-style ( blue filled rect ) is visible as well. How can i get rid of the default selection-style and only draw my red frame?

    greetings Toby

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Remove the selected state before calling paint base method.

      And please use QStyledItemDelegate - QItemDelegate is deprecated.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • TobyT Offline
        TobyT Offline
        Toby
        wrote on last edited by Toby
        #3

        Works perfectly. Many thanks!

        
        void QSelectionItemDelegate::paint( QPainter *painter_, const QStyleOptionViewItem &option_, const QModelIndex &index_ ) const
        {
            // copying QStyleOptionViewItem for modification
            QStyleOptionViewItem opt = option_;
            // remove selected-flag
            if ( opt.state & QStyle::State_Selected )
                opt.state &= ~QStyle::State_Selected;
            
            // call base for normal painting
            QStyledItemDelegate::paint( painter_, opt, index_ );
        
            // grab original selected_state and draw selection-frame
            if ( option_.state & QStyle::State_Selected )
            {
                QRect r( opt.rect.left(), opt.rect.top(), opt.rect.width() - 1, opt.rect.height() - 1 );
                painter_->setPen( Qt::red );
                painter_->drawRect( r );
            }
        }
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Toby said in QTableView / QItemDelegate customize selection:

          painter_->setPen( Qt::red );

          Even it's not applicable here you should use QPainter::Save()/restore() when you change the painter's state inside such functions to avoid spurious paintings when the painter is used afterwards.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • TobyT Offline
            TobyT Offline
            Toby
            wrote on last edited by Toby
            #5

            Thanks for the advice. :-)
            Last Question: is there a (good) way to find out whether the row above or below is also in selected_state ?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Toby said in QTableView / QItemDelegate customize selection:

              is there a (good) way to find out whether the row above or below is also in selected_state ?

              No, not with QStyleOptionViewItem. You need access to the selection model somehow.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • TobyT Offline
                TobyT Offline
                Toby
                wrote on last edited by
                #7

                ok, thx.

                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