我有一个QTableView包含数据库中的数据行.但是,设置setAlternatingRowColors(true)只会替换具有数据的行颜色 - 表的其余部分只是白色,这不是您期望的行为(例如,在任何浏览器的书签列表中查找 - 空行具有交替颜色).
有没有人知道一个workarund或Qt提供的表视图的替代方案?我已经摆弄了样式表和项目代表,结果相同.
您data()可以像这样重新实现模型的方法:
QVariant MyModel::data(const QModelIndex& index, int role) const
{
if(role == Qt::BackgroundColorRole)
return color;
...
}
Run Code Online (Sandbox Code Playgroud)
应该可以使用委托来执行相同的操作setModelData()。
您为什么不为此使用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;");
Run Code Online (Sandbox Code Playgroud)