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} How to change the border color of QTableWidgetItem
Qt 6.11 is out! See what's new in the release blog

{SOLVED} How to change the border color of QTableWidgetItem

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 6.7k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    We have a requirement where we want to change the border color of the QTableWidget item to pink when that item is clicked or selected.

    I tried implementing QItemDelegate / QStyledItemDelegate but it doesn't work.

    @class MyDelegate : public QStyledItemDelegate {
    public:
    MyDelegate( QObject *parent ) : QStyledItemDelegate( parent )
    { }

    void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
    {
        QStyledItemDelegate::initStyleOption(option, index);
    }
    
    void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    {
      QStyledItemDelegate::paint( painter, option, index );
      //if( option.state == QStyle::State_HasFocus )
      {
          QPen pen(Qt::red);
          pen.setWidth(1);
          pen.setStyle(Qt::SolidLine);
          painter->setPen( pen );
          painter->drawRect( option.rect );
      }
    }
    

    };@

    @:
    :
    :

    myTableWidget->setItemDelegate(new MyDelegate());@

    Please let me know if I am missing something.

    Regards,
    Vrushali

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2
      1. please use code tags (surrounding with @-sign)
      2. what doesn't work
      3. dumb question: did you set the delegate on the view?
      4. your delegate implementation doesn't paint anything except the border?! So no item content

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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hi,

        Thanks for pointing that out. Updated post with appropriate code tags.

        It doesn't work as in doesn't show any border around the item.

        I have set the delegate on QTableWidget as follows:

        @myTableWidget->setItemDelegate(new MyDelegate());@

        in the delegate implementation the default method is called, so wont it take care of other content?

        @QStyledItemDelegate::paint( painter, option, index );@

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          [quote author="vrushali_deshpande" date="1413892856"]Hi,
          in the delegate implementation the default method is called, so wont it take care of other content?
          @QStyledItemDelegate::paint( painter, option, index );@
          [/quote]
          thats why i asked you to add the code-tags. I overread that line ;)

          Since you want to replace the focus frame do this:
          @
          void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
          {
          bool hasFocus = (option.state & QStyle::State_HasFocus);
          option.state &= ~QStyle::State_HasFocus;
          QStyledItemDelegate::paint( painter, option, index );

               if( hasFocus )
               {
                    QRect focusRect = style->subElementRect(QStyle::SE_ItemViewItemFocusRect, &option, option.widget);
          
                  painter->save();
                      painter->setBrush( Qt::NoBrush );
          
                      QPen pen(Qt::red);
                      pen.setWidth(1);
                      pen.setStyle(Qt::SolidLine);
                      painter->setPen( pen );
          
                      painter->drawRect( focusRect );
                  painter->restore();
            }
          

          }
          @

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

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Hi,

            Thanks for the solution. Its working now :)

            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