how to change the background color of QTableWidgetItem
-
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-qtableviewRegards
Roshni -
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.
-
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()
{}
and I wanted option as
-
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.
-
As a side note, Windows style does not support setting the header background color: https://bugreports.qt.io/browse/QTBUG-31804
-
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
-
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
-
@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 }");
-
This post is deleted!