1

I've tried to set some style for check box in Qt. I know that QTableWidget has QCheckbox, but the problem is that I have no idea how to set style for it.

QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
checkBoxItem->setCheckState(Qt::Unchecked);
table->setItem(row, column, checkBoxItem);

When I use setStyleSheet() for checkBox:

checkBoxItem->setStyleSheet("...");

I get an error:

'class QTableWidgetItem' has no member named 'setStyleSheet'`

I want to do some operations with checkBox.

Here is the complete code for the first QTableWidgetItem:

for (int i = 0; i < 4; ++i) 
    m_tableWidget->setRowHeight(i, 3 * em);
    
QTableWidgetItem *item1 = new QTableWidgetItem(tr("Show Message Preview"));

if (CGlobalZone::m_showMsgPreview)
    item1->setCheckState(Qt::Checked); 
else
    item1->setCheckState(Qt::Unchecked);
    
item1->setFlags(Qt::ItemIsEnabled);
m_tableWidget->setItem(0, 0, item1);

enter image description here

Farzan Najipour
  • 2,442
  • 7
  • 42
  • 81

2 Answers2

2

You can style the indicators with QAbstractItemView::indicator { ... } (eg QTableView::indicator:checked, QTableView::indicator:unchecked etc).

You can't apply style directly to the QTableItemWidget, but you can put a stylesheet on the QTableWidget itself or a parent of it.

Hamish Moffatt
  • 826
  • 4
  • 12
0

Assuming, you already have a table widget with at least one column and at least one row:

QCheckBox *cb = new QCheckBox(tr("Check me"));
cb->setStyleSheet("background-color: rgb(0, 85, 0);");
tableWidget->setCellWidget(0, 0, cb);
Amartel
  • 4,248
  • 2
  • 15
  • 21