的Qt tableWidget細胞我就死在試圖設置背景色的Qt tableWidget。我非常希望你的幫助。 這是我的代碼。顏色
QColor colorLive(Qt::red);
ui->tableWidget->setRowCount(14);
ui->tableWidget->setColumnCount(14);
for (int g = 0; g < 15; ++g)
{
for (int i = 0; i < 15; ++i)
{
ui->tableWidget->setItem(g, i, new QTableWidgetItem);
ui->tableWidget->item(g, i)->setBackgroundColor(colorLive);
// error here
}
}
應用輸出顯示下一個:The program has unexpectedly finished.
但是,如果我通過 ui->tableWidget->item(0, 0)->setBackgroundColor(colorLive);
改變的代碼錯誤行,然後它爲一個單元。但是,實際上,我需要設置所有單元格或其中的一部分。如果你能幫助我,我將非常感激!
好,我發現了這件事。它的工作原理如下: int g = 0; while(g!= 14)int i = 0; (!I = 14) 而{ QTableWidgetItem *的newitem =新QTableWidgetItem(TR( 「%1」)的Arg( 「」)); ui-> tableWidget-> setItem(g,i,newItem); ui-> tableWidget-> item(g,i) - > setBackgroundColor(colorDeath); // ++ i; } ++ g; } – code4fun