2015-04-30 22 views
0

用下面的代碼,我們用來設置行的背景色在QTableView中盤旋在它時:在Qt5的QTableView中繪製行背景是不可能的了?

void StableTable::mouseMoveEvent (QMouseEvent * event) { 
    int row = rowAt (event->y()); 
    if(row == -1) return; 
    if (row == mHoveredRow) return; 

    QBrush background(palette().midlight()); 
    QColor backColor = background.color(); 
    background.setColor(backColor); 

    for (int col = 0; col < model()->columnCount(); col++) { 
     QModelIndex inn = model()->index(row, col); 
     model()->setData(inn, background, Qt::BackgroundRole); 

     if(mHoveredRow != -1) { 
      QModelIndex prevInn = model()->index(mHoveredRow, col); 
      model()->setData(prevInn, palette().base(), Qt::BackgroundRole); 
     } 
    } 
    mHoveredRow = row; 
} 

它好工作與Qt4.8.x但現在隨着Qt5.4無背景色IST設置了。有沒有人有線索,如果這裏發生了什麼變化?

回答

0

他們在Qt5中改變了一些東西。 BackgroundRole仍然可用,但您必須將其包裝在QVariant中。以下是我如何在表格窗口小部件項目中設置我的背景顏色。

tableItem->setData(Qt::BackgroundRole, QVariant(QColor(Qt::yellow)));