我有一个包含来自数据库的数据行的QTableView。然而,设置setAlternatingRowColors(true)仅交替显示具有数据的行的颜色-其余表格只是白色,这不是您预期的行为(例如,在任何浏览器的书签列表中查看-空行具有交替颜色)。
是否有人知道Qt提供的表视图的解决方法或替代方案?我已经尝试过样式表和项委托,但结果相同。
我有一个包含来自数据库的数据行的QTableView。然而,设置setAlternatingRowColors(true)仅交替显示具有数据的行的颜色-其余表格只是白色,这不是您预期的行为(例如,在任何浏览器的书签列表中查看-空行具有交替颜色)。
是否有人知道Qt提供的表视图的解决方法或替代方案?我已经尝试过样式表和项委托,但结果相同。
你为什么不使用Qt QSS来实现这个呢?它可以很好地工作。在这里看看:http://www.qtcentre.org/threads/42211-QTableWidget-alternating-background-color?p=263046#post263046
myTable->setAlternatingRowColors(true);
myTable->setStyleSheet("alternate-background-color: yellow;background-color: red;");
data()
方法:QVariant MyModel::data(const QModelIndex& index, int role) const
{
if(role == Qt::BackgroundColorRole)
return color;
...
}
使用setModelData()
,也可以用委托完成相同的操作。