Results 1 to 8 of 8

Thread: QTableView + BackgroundRole + Gradient

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 14:35.

Similar Threads

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