summaryrefslogtreecommitdiffstats
path: root/examples/sql/books/bookdelegate.cpp
diff options
context:
space:
mode:
authorEd Cooke <ed.cooke@qt.io>2023-11-01 14:18:14 +0100
committerEd Cooke <ed.cooke@qt.io>2023-12-14 20:06:32 +0100
commit76b2852b7cff183ba31d6b09b00cbabdffb9ba71 (patch)
tree0b50dedd8cd9c9631f9ca41c343406390a6c221f /examples/sql/books/bookdelegate.cpp
parent33254fb41f29b510d3d74dbaab60f0a67ef56d46 (diff)
Update the books example
Remove the .ui file and write the equivalent implementation. Replace the outdated star icons with new star icons. 5 empty stars are now provided and filled in depending on the rating. The new stars are SVG, however, we do not use an SvgRenderer as we cannot use the QtSvg API from QtBase directly. It is safe to assume the SVG image loading plugin is present, the worst case scenario would be empty icons, but it would still build. We instead use QIcon. For the rating, draw the star icons in a combobox, replacing the old spinbox. Update the layout by moving the table view to the bottom, and arranging the input fields at the top. The scrollbar policies have been set to Qt::ScrollBarAsNeeded for the table view. Fixes: QTBUG-118476 Pick-to: 6.7 6.6 6.5 Change-Id: I27c13534ab06e17531d155469a1cc6e7e05197af Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'examples/sql/books/bookdelegate.cpp')
-rw-r--r--examples/sql/books/bookdelegate.cpp38
1 files changed, 20 insertions, 18 deletions
diff --git a/examples/sql/books/bookdelegate.cpp b/examples/sql/books/bookdelegate.cpp
index af2a284298..6aac70b860 100644
--- a/examples/sql/books/bookdelegate.cpp
+++ b/examples/sql/books/bookdelegate.cpp
@@ -6,9 +6,8 @@
#include <QtWidgets>
BookDelegate::BookDelegate(QObject *parent)
- : QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png"))
-{
-}
+ : QSqlRelationalDelegate(parent)
+{}
void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
@@ -28,31 +27,34 @@ 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
{
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,
@@ -65,7 +67,7 @@ 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);
+ - option.rect.x()) / iconDimension), 5);
model->setData(index, QVariant(stars));
// So that the selection can change:
return false;