qt - How can I get the background color returned by model take precedence over the style -
i have qtreeview rendering qabstractitemmodel, set background of cells based on data in model. return qbrush model::data(qt::backgroundcolorrole) , works until apply style item.
setting style item (even has nothing background color, e.g. styling border) overrides color return model (the calls model querying background color made). i.e. view behaves if model never returns color.
i using qt 4.8 , cannot upgrade later version.
is there way make color returned model take precedence on style? why qt behave in such strange way - model has way more granularity , knows way more style can possibly know, why style take precedence - after all, model doesn't have return color every single cell - few specific ones?
i assume bug in qt, have opened bug report, reproducible on code:
#include <qtcore/qabstractitemmodel.h> #include <qtgui/qtreeview.h> #include <qtgui/qtableview.h> #include <qtgui/qapplication.h> class mymodel : public qabstractitemmodel { public: mymodel(qobject *parent) :qabstractitemmodel(parent){} int rowcount(const qmodelindex &parent = qmodelindex()) const { return 2; } int columncount(const qmodelindex &parent = qmodelindex()) const { return parent.isvalid() ? 0 : 2; } qvariant data(const qmodelindex &index, int role = qt::displayrole) const { if(index.row() >= 0 && index.column() >= 0) { switch(role) { case qt::displayrole: return qstring("a"); case qt::backgroundrole: return qbrush(qcolor(255 * index.row(), 255 * index.column(), 0)); default: break; } } return qvariant(); } virtual qmodelindex index(int pos, int column, const qmodelindex &parent = qmodelindex()) const { return createindex(pos, column, 0); } virtual qmodelindex parent(const qmodelindex &child) const { return qmodelindex(); } }; int main(int argc, char *argv[]) { qapplication a(argc, argv); qtreeview view; mymodel mymodel(0); view.setmodel(&mymodel); view.show(); //a.setstylesheet("qtreeview::item { border: 1px solid black; }"); return a.exec(); } if uncomment line before return, backgrounds gone.


i found workaround - still code , imho should have been handled qt itself, @ least there way overlay background default rendering, i.e. don't need reimplement default delegate (qstyleditemdelegate) does:
void delegate::paint(qpainter *painter, const qstyleoptionviewitem &option, const qmodelindex &index) const { qvariant bg = index.data(qt::backgroundrole); if(bg.isvalid()) // workaround qt bug https://bugreports.qt.io/browse/qtbug-46216 painter->fillrect(option.rect, bg.value<qbrush>()); qstyleditemdelegate::paint(painter, option, index); } if background returned model transparent (i used alpha-value of 50), nicely overlaid on style's background, alternate-row coloring or similar stuff still visible.
interesting, though, applied same trick header (in reimplemented qheaderview::paintsection) , didn't work - background visible long don't call qheaderview::paintsection - if do, painter->fillrect call ignored. i'll post separate question.
Comments
Post a Comment