alcotor
3rd May 2011, 13:20
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.
#include <QTableView>
#include <QStandardItemModel>
#include <QLinearGradient>
#include <QList>
#include <QStandardItem>
class ChartModel : public QStandardItemModel
{
public:
ChartModel():QStandardItemModel()
{
for (int i = 0; i < 15; ++i)
{
QList<QStandardItem*> row;
for (int j = 0; j < 3; ++j)
{
QStandardItem* item = new QStandardItem;
item->setData(i%10, Qt::EditRole);
row.push_back(item);
}
appendRow(row);
}
}
QVariant data(const QModelIndex& index, int role) const
{
if (role == Qt::BackgroundRole)
{
QLinearGradient grad(QPointF(0, 0), QPointF(1, 0));
grad.setColorAt(0, Qt::blue);
grad.setCoordinateMode(QGradient::ObjectBoundingMo de);
grad.setColorAt(static_cast<double>(index.row()%10)/10.0, Qt::blue);
grad.setColorAt(static_cast<double>(index.row()%10)/10.0+0.00001, Qt::white);
grad.setColorAt(1, Qt::white);
return QBrush(grad);
}
return QStandardItemModel::data(index, role);
}
};
extern void showExample()
{
QTableView* view = new QTableView;
view->setWindowTitle("charttest");
view->setWindowFlags(Qt::Tool);
view->setModel(new ChartModel);
view->show();
}
6347
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
Please see the attached screenshot and the code.
#include <QTableView>
#include <QStandardItemModel>
#include <QLinearGradient>
#include <QList>
#include <QStandardItem>
class ChartModel : public QStandardItemModel
{
public:
ChartModel():QStandardItemModel()
{
for (int i = 0; i < 15; ++i)
{
QList<QStandardItem*> row;
for (int j = 0; j < 3; ++j)
{
QStandardItem* item = new QStandardItem;
item->setData(i%10, Qt::EditRole);
row.push_back(item);
}
appendRow(row);
}
}
QVariant data(const QModelIndex& index, int role) const
{
if (role == Qt::BackgroundRole)
{
QLinearGradient grad(QPointF(0, 0), QPointF(1, 0));
grad.setColorAt(0, Qt::blue);
grad.setCoordinateMode(QGradient::ObjectBoundingMo de);
grad.setColorAt(static_cast<double>(index.row()%10)/10.0, Qt::blue);
grad.setColorAt(static_cast<double>(index.row()%10)/10.0+0.00001, Qt::white);
grad.setColorAt(1, Qt::white);
return QBrush(grad);
}
return QStandardItemModel::data(index, role);
}
};
extern void showExample()
{
QTableView* view = new QTableView;
view->setWindowTitle("charttest");
view->setWindowFlags(Qt::Tool);
view->setModel(new ChartModel);
view->show();
}
6347
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