-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraitingdelegate.cpp
More file actions
45 lines (35 loc) · 1.56 KB
/
raitingdelegate.cpp
File metadata and controls
45 lines (35 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "raitingdelegate.h"
#include <QPainter>
#include <QDebug>
RaitingDelegate::RaitingDelegate(QWidget *parent) : QStyledItemDelegate(parent) {
starImage = QImage(":/icons/Pictures/star.png");
}
void RaitingDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
QImage scaledStarImage = starImage.scaledToHeight(option.rect.height());
QRect originalRect = QRect(option.rect.topLeft(), scaledStarImage.size());
QRect translatedRect = originalRect;
// we need to repeat this once for every rating point.
for (int i = 0; i < index.data().toInt(); ++i) {
// translate (shift) our image over the right distance, or iteration * width.
translatedRect = originalRect.translated(i * scaledStarImage.width(), 0);
// draw our image.
painter->drawImage(translatedRect, scaledStarImage, scaledStarImage.rect());
}
painter->restore();
}
QWidget *RaitingDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// StarEditor *editor = new StarEditor(parent);
// connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
// return editor;
return QStyledItemDelegate::createEditor(parent, option, index);
}
QSize RaitingDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSize size(starImage.size());
size.scale(option.rect.size(), Qt::KeepAspectRatio);
size.setWidth(this->starImage.width() * 5);
return size;
}