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. How to Update QTableWidget Row Color as per Row Index Value in C++?

How to Update QTableWidget Row Color as per Row Index Value in C++?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 3.9k 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.
  • A Offline
    A Offline
    aamitgupta 0
    wrote on 10 Jul 2020, 09:58 last edited by aamitgupta 0 7 Oct 2020, 10:17
    #1

    Hi Team,
    i am new in QT development, i want to change the Row Background color as per row index, like on click on button i want to change the row background color of third number row.
    Please help me how can i do this?

    J 1 Reply Last reply 10 Jul 2020, 10:03
    0
    • A aamitgupta 0
      10 Jul 2020, 09:58

      Hi Team,
      i am new in QT development, i want to change the Row Background color as per row index, like on click on button i want to change the row background color of third number row.
      Please help me how can i do this?

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 10 Jul 2020, 10:03 last edited by
      #2

      @aamitgupta-0 https://doc.qt.io/qt-5/qtablewidget.html#item
      https://doc.qt.io/qt-5/qtablewidgetitem.html#setBackground

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • A Offline
        A Offline
        aamitgupta 0
        wrote on 10 Jul 2020, 22:11 last edited by
        #3

        Hi,

        thanks for reply.

        we tried the links you mentioned but it is not reflecting on our UI.

        AISC.jpg

        code.jpg

        Here, I this code, m_grid is QTablewidget, m_selectedFlangeShapes is a vector.

        we have tried many options like setBackground color and setData methods also but nothing is getting reflected here.

        Just to explain what we want is that when user click on import button for a single selection or selects multiple rows pressing ctrl button then all the selected row should turn to a color of our preference.

        Please let me know if some other information is also required. Many thanks in advance to get me out of this blocker in advance. Actually I have been stuck on this tasks since one week but I am not able to find its solution. Quick help will be more than life saver :-) . thanks.

        J 1 Reply Last reply 11 Jul 2020, 06:22
        0
        • A aamitgupta 0
          10 Jul 2020, 22:11

          Hi,

          thanks for reply.

          we tried the links you mentioned but it is not reflecting on our UI.

          AISC.jpg

          code.jpg

          Here, I this code, m_grid is QTablewidget, m_selectedFlangeShapes is a vector.

          we have tried many options like setBackground color and setData methods also but nothing is getting reflected here.

          Just to explain what we want is that when user click on import button for a single selection or selects multiple rows pressing ctrl button then all the selected row should turn to a color of our preference.

          Please let me know if some other information is also required. Many thanks in advance to get me out of this blocker in advance. Actually I have been stuck on this tasks since one week but I am not able to find its solution. Quick help will be more than life saver :-) . thanks.

          J Offline
          J Offline
          JonB
          wrote on 11 Jul 2020, 06:22 last edited by
          #4

          @aamitgupta-0

          we have tried many options like setBackground color and setData methods also but nothing is getting reflected here.

          @jsulm told you to to use QTableWidgetItem::setBackground() to set item background color.

          The code you have chosen to show has nothing to do with QTableWidgetItem or even QTableWidget. Please show what you tried to follow his advice. And when you do so kindly post code as text (inside ``` at start & end), not as screenshot.

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 11 Jul 2020, 07:20 last edited by mrjj 7 Nov 2020, 07:23
            #5

            Hi
            Please notice that as long as the items are still selected
            you cannot see the new color you assign as its covered by the selection color (blue/grey)!

            So while you color them you could also deselect them

            QList<QTableWidgetItem *> selList = ui->tableWidget->selectedItems();
            
                for (QTableWidgetItem *item  : selList) {
                    item->setBackgroundColor(Qt::red);
                    item->setSelected(false);
                }
            

            alt text

            1 Reply Last reply
            3
            • A Offline
              A Offline
              aamitgupta 0
              wrote on 14 Jul 2020, 03:02 last edited by
              #6

              Hi, Thank you for the reply.

              It worked at our end and now the new color is reflecting as below

              tableAISC.jpg

              But, Now I am facing a new issue as below:

              Below is the smalll section of code in style sheet which is already applied for QTable widget.

              
                 "QTableWidget::item {"
                                                                      "border-top : 2px solid rgb(231,233,234);"
                                                                      "padding: 5px;"
                                                                      "margin: 0px;"
                                                                      "text-align: left;"
                                                                      "}"
              

              Below is the image for old table when I was not getting row color changed. It am showing this to see the border lines of rows whihc is visible here.
              672e2a82-d9e3-4bb6-b080-e00df00080ba-image.png

              Problem is that when I remove Qtablwwidget section of code of style sheet then separating lines of rows disappears but row color works fine when import button is clicked. but when I keep that section of code in the style sheet then if I click on import button it does not reflect although line of rows borders show perfectly fine.

              What I need is, both row border lines and as well as rows color change when I click on import button.

              void ShapePopup::importSelectedClicked()
              {
                  QModelIndex index = m_grid->currentIndex();
                  QItemSelectionModel * select = m_grid->selectionModel();
              
                    QModelIndexList selectedList = select->selectedRows();
              
                    for(int i = 0; i < selectedList.length(); i++)
                    {
                        if(m_filterApplied)
                            m_selectedFlangeShapes.push_back(m_filteredFlangeShapes.at(selectedList.at(i).row()));
                        else
                            m_selectedFlangeShapes.push_back(m_flangeShapes.at(selectedList.at(i).row()));
              
                        for (int itr=0;itr<5;itr++) {
              
                            m_grid->item(index.row()+i,index.column()+itr)->setBackground(QColor(250,226,209));
                              m_grid->item(index.row()+i,index.column()+itr)->setSelected(false);
                        }
                    }
              m_imported = true;
              }
              
              

              Please help.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 14 Jul 2020, 06:45 last edited by mrjj
                #7

                Hi
                You cannot use setBackground and a stylesheet at the same time.
                The stylesheet will win.

                So i think you need a delegate to color the rows while using a stylesheet.
                Try this

                #include <QPainter>
                #include <QStyledItemDelegate>
                
                class HighLightDelegate : public QStyledItemDelegate
                {
                public:
                    HighLightDelegate(QObject *poParent = nullptr) :
                        QStyledItemDelegate(poParent)  {}    
                public:
                    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
                                       const QModelIndex &index) const override
                    {
                
                        QColor color = index.data(Qt::UserRole).value<QColor>();
                        if (color.isValid()) {
                            painter->fillRect(option.rect, color);
                        }
                        QStyledItemDelegate::paint(painter, option, index);
                    }
                };
                

                and then for use.

                in ctor, do it once only
                ui->tableWidget->setItemDelegate(new HighLightDelegate);
                --
                then in your 
                ---importSelectedClicked---
                    m_grid->item(index.row()+i,index.column()+itr)->setData(Qt::UserRole, QColor(250,226,209));
                
                1 Reply Last reply
                3
                • A Offline
                  A Offline
                  aamitgupta 0
                  wrote on 14 Jul 2020, 18:26 last edited by
                  #8

                  Thanks mrjj for the correct solution, i am able to implement.

                  1 Reply Last reply
                  0

                  8/8

                  14 Jul 2020, 18:26

                  • Login

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