2 Attachment(s)
QAbstractItemModel BackgroundColor Gradient Mistery....
Hi all,
I'm stuck in a bizarre error. I cannot return nothing but simple colors as BackgroundColorRole in my QAbstractItemModel's derived class
If I put some code like this in headerData method :
Code:
case Qt::BackgroundColorRole :
{
gradient.
setColorAt ( 0,
QColor::fromRgb (10*section,
20*section,
100+10*section
));
gradient.
setColorAt ( 1,
QColor::fromRgb ( 255,
255,
255 ) );
}
my grid shows it's header(s) absolutely black.
I've tested the same code in a much simpler model I've created some time ago to test my grid and model and shows the expected result :
Attachment 6173
but when I've translated those 4 lines of code into my very-much-complex application doesn't crash, but shows a "nice" black header ( and if I put this code inside data () method, shows a white background, not the black one )
Any clues ? Where can I search ? I've been debugging and my model returns in both cases the same QBrush. And when I return a QColor, it works OK...
Code:
case Qt::BackgroundColorRole :
return QColor::fromRgb (10*section,
20*section,
100+10*section
));
This is a capture of my grid, returning a Qbrush for headers & a QColor for Band header :
Attachment 6174
I'll pay some virtual beers to the one that can point me to a valid clue !! :p
Re: QAbstractItemModel BackgroundColor Gradient Mistery....
Read documantation how to use QLinearGradient! You are using it wrong! Values (0,0,1,1) are simply to small (it is pixel coordinates). Try QLinearGradient(0,0,0,60);
Re: QAbstractItemModel BackgroundColor Gradient Mistery....
I've readed the docs many times, and it works on my simple model ( have you seen the first screenshot ? )
I don't remember exactly where, but that piece of code is copied from some place in Qt's demo code, for sure.
OK, I don't follow the rules strictly, but I've tested with QLinearGradient(0,0,0,60) and I obtain the same result...
No beer for you, by now, sorry ;)
Re: QAbstractItemModel BackgroundColor Gradient Mistery....
Another possible problem is gradient.setColorAt (0, QColor::fromRgb (10*section,20*section,100+10*section ));, start from something simple like gradient.setColorAt (0,Qt::white) to see if it does work.
Re: QAbstractItemModel BackgroundColor Gradient Mistery....
QHeaderView doesn't understand complex values from the model, it's a very "stupid", in terms of features, class. It's probable it simply can't handle gradients.
Re: QAbstractItemModel BackgroundColor Gradient Mistery....
Quote:
Originally Posted by
wysota
QHeaderView doesn't understand complex values from the model, it's a very "stupid", in terms of features, class. It's probable it simply can't handle gradients.
Really ? So.. I'm a wizard !! :rolleyes:
Just kidding, take a look at the first capture... Wow !! a QheaderView showing coloured gradients !! It's possible, but I cannot find why the same code fails in my complex application
As said in Qt's documentation :
Quote:
...
Not all ItemDataRoles will have an effect on a QHeaderView. If you need to draw other roles, you can subclass QHeaderView and reimplement paintEvent(). QHeaderView respects the following item data roles: TextAlignmentRole, DisplayRole, FontRole, DecorationRole, ForegroundRole, and BackgroundRole.
Note: Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect.
As I said in my first post, QHeaderView asks my "test&simple" model and can show gradients. The problem "is out there", but I cannot find it inside my code. :confused:
I have to take a break... :mad::mad:
Re: QAbstractItemModel BackgroundColor Gradient Mistery....
Quote:
Originally Posted by
jpujolf
Really ? So.. I'm a wizard !! :rolleyes:
Just kidding, take a look at the first capture... Wow !! a QheaderView showing coloured gradients !! It's possible, but I cannot find why the same code fails in my complex application
Now go and do it for Windows.
Obviously you're doing something which can't be handled by QHeaderView. Try drawing the same gradient yourself on some label or something and see if you still get a black box. If so then you return invalid data, if not then QHeaderView simply can't handle it.
Re: QAbstractItemModel BackgroundColor Gradient Mistery....
Quote:
Originally Posted by
wysota
Now go and do it for Windows.
:(:( You're right, as usually :)
Gradients doesn't work on windows ( wuoo, wuoo, wuoo, wuoooooooo ). Delegates are not allowed on QHeaderViews, so the best approach seems to be overload paint method...
Many thanks !!!