PDA

View Full Version : QAbstractItemModel BackgroundColor Gradient Mistery....



jpujolf
31st March 2011, 07:55
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 :



case Qt::BackgroundColorRole :
{
QLinearGradient gradient ( 0, 0, 1, 1);
gradient.setColorAt ( 0,QColor::fromRgb (10*section,20*section,100+10*section ));
gradient.setColorAt ( 1, QColor::fromRgb ( 255,255,255 ) );
return QBrush ( gradient );
}


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 :
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...



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 :
6174

I'll pay some virtual beers to the one that can point me to a valid clue !! :p

MarekR22
31st March 2011, 08:19
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);

jpujolf
31st March 2011, 08:30
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 ;)

MarekR22
31st March 2011, 08:39
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.

wysota
31st March 2011, 12:33
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.

jpujolf
31st March 2011, 14:18
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 :


...
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:

wysota
31st March 2011, 14:23
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.

jpujolf
31st March 2011, 15:25
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 !!!