日志
model 里设置背景色和字体颜色等
2018-01-27 16:35
自定义了一个model,又定义了view,delegate。
想实现自定义的readOnlydelegate 背景色显示为灰色, 以显示区别。 初始想法,直接在VIEW里改,可找不到函数,接着在delegate里用了paint() 函数,变成了整个颜色覆盖了,不能显示里面的文字。 最后只好在model里改了。好无奈。 虽然也可以在model 里添加函数,来自定义各个单元格的字体、颜色、背景色等,但这个和MVC的思想是不是有点违背?反过来想,字体颜色等也是数据,当然得存在model QVariant TableModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); switch (role) { case Qt::BackgroundColorRole:{ if (index.column () > 2) { return QColor("#FAF0E6"); } else { return QVariant(); } } case Qt::ForegroundRole:{ return QBrush(Qt::blue); } case Qt::DisplayRole: { //文本 //... return tableGridData.at(index.row()).at(index.column()); } case Qt::EditRole: { //对齐方式 //... return tableGridData.at(index.row()).at(index.column()); } default: return QVariant(); } } |
下一篇: 无