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. QTableWidget replace dotted selected row with border

QTableWidget replace dotted selected row with border

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.0k Views 1 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.
  • E Offline
    E Offline
    E106JZ
    wrote on last edited by
    #1

    For a QTableWidget, I'm trying to replace the default dotted border around a selected row so that it extends to the entire row and is a black border instead. I've searched the forums and google but I can't find any examples of what I want to achieve which is this:

    22c08c6d-bd89-4271-9c0e-59402d33d07d-image.png

    I tried setting the outline property in the stylesheets but it didn't do anything.
    Does anyone know how to do this?

    Thanks

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

      Hi
      The dotted is actually a focus rect not selection. Different concept.
      Anyway,
      you can use a delegate to do custom drawing to get that effect

      class DrawBorderDelegate : public QStyledItemDelegate
      {
          QTableView *table;
      public:
          DrawBorderDelegate( QObject *parent, QTableView *theTable ) : QStyledItemDelegate( parent ),
              table(theTable)  {}
          void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
          {
      
              // kill the focus rect
              QStyleOptionViewItem option2 = option; // its const..
              option2.state = option.state & (~QStyle::State_HasFocus);
      
              QStyledItemDelegate::paint( painter, option2, index );
      
              // if selected row paint borders
              if (  table->currentIndex().row() == index.row() ) {
                  const QRect rect( option.rect );
                  painter->setPen(QPen(Qt::red, 2));
                  painter->drawLine( rect.topLeft(), rect.topRight() );
                  painter->drawLine( rect.bottomLeft(), rect.bottomRight() );
      
                  // Draw left edge of left-most cell
                  if ( index.column() == 0 )
                      painter->drawLine( rect.topLeft(), rect.bottomLeft() );
      
                  // Draw right edge of right-most cell
                  if ( index.column() == index.model()->columnCount() - 1 )
                      painter->drawLine( rect.topRight(), rect.bottomRight() );
              }
      
      
          }
      
      }; // DrawBorderDelegate
      .. in cpp
       ui->tableWidget->setItemDelegate( new DrawBorderDelegate( this,  ui->tableWidget ) );
      

      alt text

      Design note. this was fast made so i just gave the QTableWidget as a parameter to know the current row.
      That couples it to that type of view which is not optimal.

      E 2 Replies Last reply
      1
      • mrjjM mrjj

        Hi
        The dotted is actually a focus rect not selection. Different concept.
        Anyway,
        you can use a delegate to do custom drawing to get that effect

        class DrawBorderDelegate : public QStyledItemDelegate
        {
            QTableView *table;
        public:
            DrawBorderDelegate( QObject *parent, QTableView *theTable ) : QStyledItemDelegate( parent ),
                table(theTable)  {}
            void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
            {
        
                // kill the focus rect
                QStyleOptionViewItem option2 = option; // its const..
                option2.state = option.state & (~QStyle::State_HasFocus);
        
                QStyledItemDelegate::paint( painter, option2, index );
        
                // if selected row paint borders
                if (  table->currentIndex().row() == index.row() ) {
                    const QRect rect( option.rect );
                    painter->setPen(QPen(Qt::red, 2));
                    painter->drawLine( rect.topLeft(), rect.topRight() );
                    painter->drawLine( rect.bottomLeft(), rect.bottomRight() );
        
                    // Draw left edge of left-most cell
                    if ( index.column() == 0 )
                        painter->drawLine( rect.topLeft(), rect.bottomLeft() );
        
                    // Draw right edge of right-most cell
                    if ( index.column() == index.model()->columnCount() - 1 )
                        painter->drawLine( rect.topRight(), rect.bottomRight() );
                }
        
        
            }
        
        }; // DrawBorderDelegate
        .. in cpp
         ui->tableWidget->setItemDelegate( new DrawBorderDelegate( this,  ui->tableWidget ) );
        

        alt text

        Design note. this was fast made so i just gave the QTableWidget as a parameter to know the current row.
        That couples it to that type of view which is not optimal.

        E Offline
        E Offline
        E106JZ
        wrote on last edited by
        #3

        @mrjj Wow thanks!

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          The dotted is actually a focus rect not selection. Different concept.
          Anyway,
          you can use a delegate to do custom drawing to get that effect

          class DrawBorderDelegate : public QStyledItemDelegate
          {
              QTableView *table;
          public:
              DrawBorderDelegate( QObject *parent, QTableView *theTable ) : QStyledItemDelegate( parent ),
                  table(theTable)  {}
              void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
              {
          
                  // kill the focus rect
                  QStyleOptionViewItem option2 = option; // its const..
                  option2.state = option.state & (~QStyle::State_HasFocus);
          
                  QStyledItemDelegate::paint( painter, option2, index );
          
                  // if selected row paint borders
                  if (  table->currentIndex().row() == index.row() ) {
                      const QRect rect( option.rect );
                      painter->setPen(QPen(Qt::red, 2));
                      painter->drawLine( rect.topLeft(), rect.topRight() );
                      painter->drawLine( rect.bottomLeft(), rect.bottomRight() );
          
                      // Draw left edge of left-most cell
                      if ( index.column() == 0 )
                          painter->drawLine( rect.topLeft(), rect.bottomLeft() );
          
                      // Draw right edge of right-most cell
                      if ( index.column() == index.model()->columnCount() - 1 )
                          painter->drawLine( rect.topRight(), rect.bottomRight() );
                  }
          
          
              }
          
          }; // DrawBorderDelegate
          .. in cpp
           ui->tableWidget->setItemDelegate( new DrawBorderDelegate( this,  ui->tableWidget ) );
          

          alt text

          Design note. this was fast made so i just gave the QTableWidget as a parameter to know the current row.
          That couples it to that type of view which is not optimal.

          E Offline
          E Offline
          E106JZ
          wrote on last edited by
          #4

          @mrjj said in QTableWidget replace dotted selected row with border:

          ui->tableWidget->setItemDelegate( new DrawBorderDelegate( this, ui->tableWidget ) );

          I've noticed that border looks thicker on the bottom than the top. Also it looks like the painter doesn't draw over the table grid lines. Why does this happen?

          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