2013-10-05 89 views
1

在我的Qt項目中,我的對話框中有表格視圖,我使用代理模型和表模型 從SQL設置其數據庫。現在我想改變我的表格視圖列的顏色[只列[3,4,5]。 搜索互聯網後,終於我成功地改變了顏色,但整個表視圖的顏色。我正在使用下面的代碼:Qt TableView與StyleSheet

ui->tableView->setStyleSheet("background-color: yellow"); 

我應該寫什麼來使黃色,只有一些列?

我想我應該改變「背景顏色」的東西,但我不是很擅長CSS,我不知道要搜索什麼。

回答

1

你應該根據單元格顏色,而不是QtableWidgetQtableModel功能:

void MyParentWidget::highlightCell(const QModelIndex &cellIndex) 
{ 
for(int i=0; i<cellIndex.model()->columnCount(); i++) 
{ 
    for(int j=0; j<cellIndex.model()->rowCount(); j++) 
    { 
    if(i == cellIndex.column() && j == cellIndex.row()) 
    { 
    ((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(),  i)->setData(QBrush(Qt::yellow), 
Qt::BackgroundRole); 
    } 
    else 
    { 
    ((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(), i)->setData(QBrush(Qt::white), 
Qt::BackgroundRole); 
    } 
    } 
} 
} 
+0

感謝所有的幫助,但沒有任何作品,我試過你的建議。我甚至沒有在我的intellisense中使用 - > item()函數。 – user2521257

+0

使用文檔.. – PersianGulf