Results 1 to 8 of 8

Thread: QTableView + BackgroundRole + Gradient

  1. #1
    Join Date
    May 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTableView + BackgroundRole + Gradient

    I could use a little help to verify if I'm missing something, or if this is a Qt-bug.

    Please see the attached screenshot and the code.

    Qt Code:
    1. #include <QTableView>
    2. #include <QStandardItemModel>
    3. #include <QLinearGradient>
    4. #include <QList>
    5. #include <QStandardItem>
    6.  
    7. class ChartModel : public QStandardItemModel
    8. {
    9. public:
    10. ChartModel():QStandardItemModel()
    11. {
    12. for (int i = 0; i < 15; ++i)
    13. {
    14. QList<QStandardItem*> row;
    15. for (int j = 0; j < 3; ++j)
    16. {
    17. item->setData(i%10, Qt::EditRole);
    18. row.push_back(item);
    19. }
    20. appendRow(row);
    21. }
    22. }
    23.  
    24. QVariant data(const QModelIndex& index, int role) const
    25. {
    26. if (role == Qt::BackgroundRole)
    27. {
    28. QLinearGradient grad(QPointF(0, 0), QPointF(1, 0));
    29. grad.setColorAt(0, Qt::blue);
    30. grad.setCoordinateMode(QGradient::ObjectBoundingMode);
    31. grad.setColorAt(static_cast<double>(index.row()%10)/10.0, Qt::blue);
    32. grad.setColorAt(static_cast<double>(index.row()%10)/10.0+0.00001, Qt::white);
    33. grad.setColorAt(1, Qt::white);
    34. return QBrush(grad);
    35. }
    36. return QStandardItemModel::data(index, role);
    37. }
    38. };
    39.  
    40. extern void showExample()
    41. {
    42. QTableView* view = new QTableView;
    43. view->setWindowTitle("charttest");
    44. view->setWindowFlags(Qt::Tool);
    45. view->setModel(new ChartModel);
    46. view->show();
    47. }
    To copy to clipboard, switch view to plain text mode 

    charttest.jpg

    It's obvious what effect I want to achive. I want to use the gradient to "draw" something.
    I like it because it is very powerful yet easy to maintain. However, this example only works for the first column. After that, the Brush does not get the correct offset (or something), and the Gradient does not get the color right.
    I expect column 2 and 3 to show the exact same "histogram", but they do not.

    Any idea why this is not working?

    I would really prefer this approach over any custom QPainter stuff. I'm on Qt 4.6.2, and I use QStyledItemDelegate, not QItemDelegate, so overwriting QItemDelegate::drawBackground is not where I want to go.


    Thanks for any help
    Last edited by alcotor; 3rd May 2011 at 13:35.

  2. #2
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows
    Thanks
    4
    Thanked 5 Times in 4 Posts

    Default Re: QTableView + BackgroundRole + Gradient

    Well QItemDelegate::drawBackground() is pointless as there is a bug in Qt-this function has been forgotten to be declared as virtual :/
    About your code,I'd write it like:
    Qt Code:
    1. if(role == Qt::BackgroundRole && index.isValid())
    To copy to clipboard, switch view to plain text mode 
    but even without this 'purism' it seems to be OK.For the test,screw up the gradient and return a simple QColor to see if it works.

  3. #3
    Join Date
    May 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: QTableView + BackgroundRole + Gradient

    Quote Originally Posted by MasterBLB View Post
    ...For the test,screw up the gradient and return a simple QColor to see if it works.
    The simple QColor case works fine. I can light up the table like a christmas-tree.

    With the gradients I can actually "draw" something meaningful. You could create pie-charts, horizontal and vertical bar charts, and more, and since all is relative, those "charts" resize with the column-witdh. All this works in the first column, but not in the second.
    ObjectBoundingMode does not seem to work correctly, or I'm doing something wrong.


  4. #4
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows
    Thanks
    4
    Thanked 5 Times in 4 Posts

    Default Re: QTableView + BackgroundRole + Gradient

    I've found something: change the second color from white to other,yellow for example.It seems that everything what's wrong lies inside the gradient itself,not the view/model
    Also,try to comment out lines:
    Qt Code:
    1. grad.setColorAt(static_cast<double>(index.row()%10)/10.0, Qt::blue);
    2. grad.setColorAt(static_cast<double>(index.row()%10)/10.0+0.00001, Qt::white);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView + BackgroundRole + Gradient

    Yes, the problem is within the Gradient. Exactly!

    Updated example (smaller):
    Qt Code:
    1. #include <QTableView>
    2. #include <QStandardItemModel>
    3. #include <QLinearGradient>
    4. #include <QList>
    5. #include <QStandardItem>
    6.  
    7. class ChartModel : public QStandardItemModel
    8. {
    9. public:
    10. ChartModel():QStandardItemModel()
    11. {
    12. for (int i = 0; i < 10; ++i)
    13. {
    14. QList<QStandardItem*> row;
    15. for (int j = 0; j < 2; ++j)
    16. {
    17. item->setData("50 %", Qt::EditRole);
    18. row.push_back(item);
    19. }
    20. appendRow(row);
    21. }
    22. }
    23.  
    24. QVariant data(const QModelIndex& index, int role) const
    25. {
    26. if (role == Qt::BackgroundRole && index.isValid())
    27. {
    28. QLinearGradient grad(QPointF(0, 0), QPointF(1, 0));
    29. grad.setCoordinateMode(QGradient::ObjectBoundingMode);
    30. grad.setColorAt(0, Qt::blue);
    31. grad.setColorAt(0.5, Qt::blue);
    32. grad.setColorAt(0.50001, Qt::white);
    33. grad.setColorAt(1, Qt::white);
    34. return QBrush(grad);
    35. }
    36. return QStandardItemModel::data(index, role);
    37. }
    38. };
    39.  
    40. extern void showExample()
    41. {
    42. QTableView* view = new QTableView;
    43. view->setWindowTitle("charttest");
    44. view->setWindowFlags(Qt::Tool);
    45. view->setModel(new ChartModel);
    46. view->show();
    47. }
    To copy to clipboard, switch view to plain text mode 

    In this example I want the blue to end at 50% of the width of the column, and use white for the other 50%. And to do that, I use a LinearGradient
    0 (blue) ---- 0.5 (blue) - 0.500001 (white) ---- 1.0 (white).
    The actual gradient effect is only between 0.5 and 0.500001.

    charttest2.PNG

    Anyway, here is another screenshot. Maybe someone has an idea?

    Thanks

  6. #6
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows
    Thanks
    4
    Thanked 5 Times in 4 Posts

    Default Re: QTableView + BackgroundRole + Gradient

    Alcotor how the griadient in the first colum behaves when you're resizing it?Has it still the proper size?

  7. #7
    Join Date
    May 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView + BackgroundRole + Gradient

    Quote Originally Posted by MasterBLB View Post
    Alcotor how the griadient in the first colum behaves when you're resizing it?Has it still the proper size?
    Yes.

    charttest3.jpg

    You can see how the Gradient aligns at 50% under the header "1", which is center aligned. However, only for the first column. When you resize the header, you notice some interaction between the gradient-calculation of column two, and the width of column one, which I think is the bug.

    Well, I'm not ready to give up though!


  8. #8
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows
    Thanks
    4
    Thanked 5 Times in 4 Posts

    Default Re: QTableView + BackgroundRole + Gradient

    I've discovered that
    Qt Code:
    1. grad.setSpread(QGradient::RepeatSpread);
    To copy to clipboard, switch view to plain text mode 
    helps a bit until the columns are not resized.

Similar Threads

  1. qt directFB gradient
    By goli in forum Qt Programming
    Replies: 0
    Last Post: 12th April 2011, 16:01
  2. Add a QGraphicsTextItem with a gradient
    By harmodrew in forum Newbie
    Replies: 5
    Last Post: 16th November 2010, 17:50
  3. getting gradient colors
    By venkat.godavarthi in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2009, 13:36
  4. Gradient in histogram
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 1st August 2009, 11:48
  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
  •  
Qt is a trademark of The Qt Company.