diff options
Diffstat (limited to 'examples/sql/books/bookdelegate.cpp')
-rw-r--r-- | examples/sql/books/bookdelegate.cpp | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/examples/sql/books/bookdelegate.cpp b/examples/sql/books/bookdelegate.cpp index af2a284298..ead9f9e7db 100644 --- a/examples/sql/books/bookdelegate.cpp +++ b/examples/sql/books/bookdelegate.cpp @@ -3,15 +3,13 @@ #include "bookdelegate.h" -#include <QtWidgets> +#include <QMouseEvent> +#include <QPainter> +#include <QSpinBox> -BookDelegate::BookDelegate(QObject *parent) - : QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png")) -{ -} - -void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, - const QModelIndex &index) const +void BookDelegate::paint(QPainter *painter, + const QStyleOptionViewItem &option, + const QModelIndex &index) const { if (index.column() != 5) { QSqlRelationalDelegate::paint(painter, option, index); @@ -28,31 +26,33 @@ void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, option.rect, option.palette.color(cg, QPalette::Highlight)); - int rating = model->data(index, Qt::DisplayRole).toInt(); - int width = star.width(); - int height = star.height(); - int x = option.rect.x(); + const int rating = model->data(index, Qt::DisplayRole).toInt(); + const int width = iconDimension; + const int height = width; + // add cellPadding / 2 to center the stars in the cell + int x = option.rect.x() + cellPadding / 2; int y = option.rect.y() + (option.rect.height() / 2) - (height / 2); - for (int i = 0; i < rating; ++i) { - painter->drawPixmap(x, y, star); + + QIcon starIcon(QStringLiteral(":images/star.svg")); + QIcon starFilledIcon(QStringLiteral(":images/star-filled.svg")); + + for (int i = 0; i < 5; ++i) { + if (i < rating) + starFilledIcon.paint(painter, QRect(x, y, width, height)); + else + starIcon.paint(painter, QRect(x, y, width, height)); x += width; } } - - QPen pen = painter->pen(); - painter->setPen(option.palette.color(QPalette::Mid)); - painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); - painter->drawLine(option.rect.topRight(), option.rect.bottomRight()); - painter->setPen(pen); } QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option, - const QModelIndex &index) const + const QModelIndex &index) const { if (index.column() == 5) - return QSize(5 * star.width(), star.height()) + QSize(1, 1); + return QSize(5 * iconDimension, iconDimension) + QSize(cellPadding, cellPadding); // Since we draw the grid ourselves: - return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1); + return QSqlRelationalDelegate::sizeHint(option, index) + QSize(cellPadding, cellPadding); } bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, @@ -64,8 +64,8 @@ bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, if (event->type() == QEvent::MouseButtonPress) { QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); - int stars = qBound(0, int(0.7 + qreal(mouseEvent->position().toPoint().x() - - option.rect.x()) / star.width()), 5); + int stars = qBound(0, int(0.7 + qreal(mouseEvent->position().x() + - option.rect.x()) / iconDimension), 5); model->setData(index, QVariant(stars)); // So that the selection can change: return false; |