Unsolved QTableWidget set background color segfault
-
Hi All,
I'm trying to make one row of a table red, to signal an invalid combination. I have tried the following, but this yields a segfault:
QTableWidget *tableWidget;
tableWidget = new QTableWidget(0,9,this);
tableWidget->insertRow(row);
tableWidget->setCellWidget(row,IDXSTART,new QDateTimeEdit);
tableWidget->item(row,0)->setBackgroundColor(QColor(255,0,0,127)); //segfault
qtablewidget.h
inline void setBackgroundColor(const QColor &color)
{ setData(Qt::BackgroundColorRole, color); }How can I set the background color of a row of my table to red?
Kind regards,
Cedric -
@cdwijs said:
row
Are you sure this variable has a valid value?
I think item(row,0) returns NULL and u then call NULL->setBackgroundColor
which will crash. -
I think you'll need a call to QTableWidget::setItem prior to the QTableWidget::itemAt call. Something like:
tableWidget->setItem(row, 0, new QTableWidgetItem(tr("test"))); ... QTableWidgetItem * const item = tableWidget->item(row,0); if (item) setBackgroundColor(QColor(255,0,0,127));
pc.
-
@Paul-Colby said:
I think you'll need a call to QTableWidget::setItem prior to the QTableWidget::itemAt call
As Paul said. You need to create an item first.
I would suggest to always check your expectations:QTableWidgetItem *const item = tableWidget->item(0,0);
Q_CHECK_PTR( item );
item->setBackgroundColor(QColor(255,0,0,127));