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 change the background color of QTableWidgetItem
Forum Updated to NodeBB v4.3 + New Features

how to change the background color of QTableWidgetItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 9.8k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    Hi All

    How to set the background color of QTableWidget and as well setCellWidget
    https://forum.qt.io/topic/97500/how-to-get-gray-background-headers-of-qtableview

    Regards
    Roshni

    1 Reply Last reply
    0
    • MasterBLBM Offline
      MasterBLBM Offline
      MasterBLB
      wrote on last edited by MasterBLB
      #2

      use something like this:

      QAbstractItemModel *model = yourTable->horizontalHeader()->model();
      model->setData(model->index(0, 0), Qt::BackgroundRole, Qt::Grey);
      
      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        For each of the item set like follows.
        QTableWidgetItem::setData(Qt::BackgroundRole,Qt::grey)

        I think your question is duplicate of other question. Please read the documentation based on the direction given by the group.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        9
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on last edited by
          #4

          One more thing last

          with this code

          #include "mainwindow.h"
          #include <QHeaderView>
          #include <QTableWidgetItem>
          #include <QLabel>
          #include <QPushButton>

          myWidget::myWidget(QWidget *parent) : QWidget(NULL) {
          hbox = new QHBoxLayout;

          QLabel* label = new QLabel;

          label->setText("Options");
          hbox->addWidget(label);

          QPushButton* push = new QPushButton("+");
          hbox->addWidget(push);
          push->setMinimumHeight(20);
          label->setMaximumHeight(20);

          setMinimumWidth(100);
          setStyleSheet("background-color:gray");
          setLayout(hbox);

          }

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent)
          {

          QTableWidget * t = new QTableWidget;
          t->setColumnCount(5);
          t->setRowCount(3);
          QStringList headerLabels;

          QString col1 = "No of Patterns";
          QString col2 = "Set";
          QString col3 = "Type";
          QString col4 = " Pattern rules : pattern_1 : pattern_2 ";
          QString col5 = "Options";
          headerLabels << col1 << col2 << col3 << col4 << col5;
          t->setHorizontalHeaderLabels(headerLabels);

          t->setStyleSheet("QHeaderView::section { background-color:'light grey' }");

          QColor color(QColor("light grey"));
          QTableWidgetItem* item = new QTableWidgetItem("23");
          item->setData(Qt::BackgroundRole,color);

          QColor color1(QColor("light grey"));
          QTableWidgetItem* item1 = new QTableWidgetItem("9");
          item1->setData(Qt::BackgroundRole,color1);

          QTableWidgetItem* item2 = new QTableWidgetItem("Set1");
          item2->setData(Qt::BackgroundRole,color);

          QTableWidgetItem* item3 = new QTableWidgetItem("Set2");
          item3->setData(Qt::BackgroundRole,color);

          t->setItem(0,0,item);
          t->setItem(1,0,item1);
          t->setItem(0,1,item2);
          t->setItem(1,1,item3);

          QComboBox* combox = new QComboBox;
          t->setCellWidget(0,2,combox);
          combox->addItem(QString(("T1")));
          combox->addItem(QString(("T2")));

          QComboBox* combox_1 = new QComboBox;
          t->setCellWidget(1,2,combox_1);
          combox_1->addItem(QString(("T1")));
          combox_1->addItem(QString(("T2")));

          QLineEdit * edit = new QLineEdit;
          edit->setText(" aaa: aaa ");
          t->setCellWidget(0,3,edit);

          QLineEdit * edit1 = new QLineEdit;
          edit1->setText(" bbb: bbb ");
          t->setCellWidget(1,3,edit1);

          myWidget* m = new myWidget;
          t->setCellWidget(0,4,m);

          QTableWidgetItem* item11 = t->item(0,4);
          QColor color2(QColor("light grey"));

          //item11->setBackgroundColor(color2);

          /myWidget n = new myWidget;
          t->setCellWidget(1,4,n);
          t->item(1,4)->setBackground(QColor("light grey"));*/

          //QColor color(QColor("light grey"));
          //item->setData(Qt::BackgroundRole,color);

          t->resizeColumnsToContents();
          t->verticalHeader()->hide();
          t->show();

          }

          MainWindow::~MainWindow()
          {

          }

          https://ibb.co/g9vmWhZ

          and I wanted option as

          https://ibb.co/bbG20Sg

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            Read the documentation on how delegates work. There are lot of examples exist in the examples directory. You should be able to check & make it happen.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            1
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              As a side note, Windows style does not support setting the header background color: https://bugreports.qt.io/browse/QTBUG-31804

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #7

                Using the same code

                #include "mainwindow.h"
                #include <QHeaderView>
                #include <QTableWidgetItem>
                #include <QLabel>
                #include <QPushButton>

                myWidget::myWidget(QWidget *parent) : QWidget(NULL) {
                hbox = new QHBoxLayout;

                QLabel* label = new QLabel;

                label->setText("Options");
                hbox->addWidget(label);

                QPushButton* push = new QPushButton("+");
                hbox->addWidget(push);
                push->setMinimumHeight(10);
                push->setMinimumWidth(10);
                push->setMaximumWidth(20);
                label->setMaximumHeight(50);
                label->setMinimumHeight(30);

                setMinimumWidth(100);
                setStyleSheet("background-color:gray");
                setLayout(hbox);

                }

                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent)
                {

                QTableWidget * t = new QTableWidget;
                t->setColumnCount(5);
                t->setRowCount(3);
                QStringList headerLabels;

                QString col1 = "No of Patterns";
                QString col2 = "Set";
                QString col3 = "Type";
                QString col4 = " Pattern rules : pattern_1 : pattern_2 ";
                QString col5 = "Options";
                headerLabels << col1 << col2 << col3 << col4 << col5;
                t->setHorizontalHeaderLabels(headerLabels);

                t->setStyleSheet("QHeaderView::section { background-color:'light grey' }");

                QColor color(QColor("light grey"));
                QTableWidgetItem* item = new QTableWidgetItem("23");
                item->setData(Qt::BackgroundRole,color);

                QColor color1(QColor("light grey"));
                QTableWidgetItem* item1 = new QTableWidgetItem("9");
                item1->setData(Qt::BackgroundRole,color1);

                QTableWidgetItem* item2 = new QTableWidgetItem("Set1");
                item2->setData(Qt::BackgroundRole,color);

                QTableWidgetItem* item3 = new QTableWidgetItem("Set2");
                item3->setData(Qt::BackgroundRole,color);

                t->setItem(0,0,item);
                t->setItem(1,0,item1);
                t->setItem(0,1,item2);
                t->setItem(1,1,item3);

                QComboBox* combox = new QComboBox;
                t->setCellWidget(0,2,combox);
                combox->addItem(QString(("T1")));
                combox->addItem(QString(("T2")));

                QComboBox* combox_1 = new QComboBox;
                t->setCellWidget(1,2,combox_1);
                combox_1->addItem(QString(("T1")));
                combox_1->addItem(QString(("T2")));

                QLineEdit * edit = new QLineEdit;
                edit->setText(" aaa: aaa ");
                t->setCellWidget(0,3,edit);

                QLineEdit * edit1 = new QLineEdit;
                edit1->setText(" bbb: bbb ");
                t->setCellWidget(1,3,edit1);

                myWidget* m = new myWidget;
                t->setCellWidget(0,4,m);

                QTableWidgetItem* item11 = t->item(0,4);
                QColor color2(QColor("light grey"));

                //item11->setBackgroundColor(color2);

                /myWidget n = new myWidget;
                t->setCellWidget(1,4,n);
                t->item(1,4)->setBackground(QColor("light grey"));*/

                //QColor color(QColor("light grey"));
                //item->setData(Qt::BackgroundRole,color);

                t->resizeColumnsToContents();
                t->verticalHeader()->hide();
                t->show();

                }

                MainWindow::~MainWindow()
                {

                }

                How to get the boundary color

                of QColor color(QColor("light grey"));
                QTableWidgetItem* item = new QTableWidgetItem("23");
                item->setData(Qt::BackgroundRole,color);

                I am getting image as
                0_1544778733866_T1.PNG

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on last edited by
                  #8

                  I tried to set the setStyleSheet for QTableWidget ad grid line color. but it did not help

                  Can u please help me in sample code of how to set grid line color of QtableWidget using setStyleSheet

                  RatzzR 1 Reply Last reply
                  0
                  • Q Qt Enthusiast

                    I tried to set the setStyleSheet for QTableWidget ad grid line color. but it did not help

                    Can u please help me in sample code of how to set grid line color of QtableWidget using setStyleSheet

                    RatzzR Offline
                    RatzzR Offline
                    Ratzz
                    wrote on last edited by
                    #9

                    @Qt-Enthusiast said in how to change the background color of QTableWidgetItem:

                    help me in sample code of how to set grid line color of QtableWidget using setStyleSheet

                    This works for me.

                    ui->tableWidget->setStyleSheet("QTableWidget { gridline-color: yellow }");

                    --Alles ist gut.

                    1 Reply Last reply
                    2
                    • Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on last edited by
                      #10
                      This post is deleted!
                      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