无法在Qt5的QTableView中绘制行背景?
使用以下代码,我们用来设置将鼠标悬停在QTableView上时该行的背景色:无法在Qt5的QTableView中绘制行背景?,qt5,qtableview,Qt5,Qtableview,使用以下代码,我们用来设置将鼠标悬停在QTableView上时该行的背景色: void StableTable::mouseMoveEvent ( QMouseEvent * event ) { int row = rowAt ( event->y() ); if( row == -1 ) return; if ( row == mHoveredRow ) return; QBrush background(palette().midlight());
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;
}
void StableTable::mouseMoveEvent(QMouseEvent*event){
int row=rowAt(事件->y());
如果(行==-1)返回;
if(row==mHoveredRow)返回;
QBrush背景(调色板().midlight());
QColor backColor=background.color();
背景色(背景色);
对于(int col=0;colcolumnCount();col++){
QModelIndex inn=model()->索引(行、列);
model()->setData(inn,background,Qt::BackgroundRole);
如果(mHoveredRow!=-1){
QModelIndex prevInn=model()->索引(mHoveredRow,col);
model()->setData(prevInn,palete().base(),Qt::BackgroundRole);
}
}
mh=行;
}
它在Qt4.8.x上运行良好,但现在在Qt5.4上不再设置背景色。有人知道这里是否发生了变化吗?他们在Qt5中改变了一些事情。BackgroundRole仍然可用,但您必须将其包装在QVariant中。下面是我如何在表格小部件项中设置背景色的
tableItem->setData(Qt::BackgroundRole, QVariant(QColor(Qt::yellow)));