Results 1 to 8 of 8

Thread: QAbstractItemModel BackgroundColor Gradient Mistery....

  1. #1
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Qt Code:
    1. case Qt::BackgroundColorRole :
    2. {
    3. QLinearGradient gradient ( 0, 0, 1, 1);
    4. gradient.setColorAt ( 0,QColor::fromRgb (10*section,20*section,100+10*section ));
    5. gradient.setColorAt ( 1, QColor::fromRgb ( 255,255,255 ) );
    6. return QBrush ( gradient );
    7. }
    To copy to clipboard, switch view to plain text mode 

    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 :
    Captura-Simple Extended Grid TEST ( extending Qt's (c) Simple Tree model ).jpg

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

    Qt Code:
    1. case Qt::BackgroundColorRole :
    2. return QColor::fromRgb (10*section,20*section,100+10*section ));
    To copy to clipboard, switch view to plain text mode 

    This is a capture of my grid, returning a Qbrush for headers & a QColor for Band header :
    Captura-Estudios Planificación y Costes.jpg

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

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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);

  3. #3
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractItemModel BackgroundColor Gradient Mistery....

    Quote Originally Posted by wysota View Post
    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 !!

    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.

    I have to take a break...

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractItemModel BackgroundColor Gradient Mistery....

    Quote Originally Posted by jpujolf View Post
    Really ? So.. I'm a wizard !!

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    jpujolf (31st March 2011)

  9. #8
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractItemModel BackgroundColor Gradient Mistery....

    Quote Originally Posted by wysota View Post
    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 !!!

Similar Threads

  1. HeaderView BackgroundColor
    By dragon in forum Qt Programming
    Replies: 10
    Last Post: 9th July 2008, 19:22
  2. Replies: 1
    Last Post: 17th March 2008, 14:02
  3. Backgroundcolor QLCDNumber in Windows?
    By Teuniz in forum Qt Programming
    Replies: 4
    Last Post: 30th August 2006, 15:09
  4. QTreeWidget backgroundcolor
    By vmferreira in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 20:21
  5. gradient background
    By drkbkr in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2006, 16:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.