From 1cfe064632099e036f964832c23afe266e54d59e Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Thu, 22 Nov 2018 21:21:25 +0100 Subject: [PATCH] Cleanup StarDelegate example Cleanup the StarDelegate example: - use QStyledItemDelegate instead QItemDelegate - use nullptr and other useful c++11 constructs - include the correct headers Change-Id: If2f65fe7cbdcdd4571d10ffa98d36eeab7836bbb Reviewed-by: Sze Howe Koh Reviewed-by: Paul Wicking Reviewed-by: Luca Beldi --- examples/widgets/doc/src/stardelegate.qdoc | 14 +++++----- .../widgets/itemviews/stardelegate/main.cpp | 5 +--- .../itemviews/stardelegate/stardelegate.cpp | 8 +++--- .../itemviews/stardelegate/stardelegate.h | 3 +-- .../itemviews/stardelegate/stareditor.cpp | 20 +++++++------- .../itemviews/stardelegate/stareditor.h | 5 ++-- .../itemviews/stardelegate/starrating.cpp | 27 ++++++++----------- .../itemviews/stardelegate/starrating.h | 8 +++--- 8 files changed, 40 insertions(+), 50 deletions(-) diff --git a/examples/widgets/doc/src/stardelegate.qdoc b/examples/widgets/doc/src/stardelegate.qdoc index 44d17662ca9..0b91723a512 100644 --- a/examples/widgets/doc/src/stardelegate.qdoc +++ b/examples/widgets/doc/src/stardelegate.qdoc @@ -42,11 +42,11 @@ editing takes place. Delegates are subclasses of QAbstractItemDelegate. Qt provides - QItemDelegate, which inherits QAbstractItemDelegate and handles + QStyledItemDelegate, which inherits QAbstractItemDelegate and handles the most common data types (notably \c int and QString). If we need to support custom data types, or want to customize the rendering or the editing for existing data types, we can subclass - QAbstractItemDelegate or QItemDelegate. See \l{Delegate Classes} + QAbstractItemDelegate or QStyledItemDelegate. See \l{Delegate Classes} for more information about delegates, and \l{Model/View Programming} if you need a high-level introduction to Qt's model/view architecture (including delegates). @@ -62,9 +62,9 @@ expressed as stars, such as "2 out of 5 stars" or "5 out of 6 stars". - \li \c StarDelegate inherits QItemDelegate and provides support + \li \c StarDelegate inherits QStyledItemDelegate and provides support for \c StarRating (in addition to the data types already - handled by QItemDelegate). + handled by QStyledItemDelegate). \li \c StarEditor inherits QWidget and is used by \c StarDelegate to let the user edit a star rating using the mouse. @@ -80,12 +80,12 @@ \snippet itemviews/stardelegate/stardelegate.h 0 All public functions are reimplemented virtual functions from - QItemDelegate to provide custom rendering and editing. + QStyledItemDelegate to provide custom rendering and editing. \section1 StarDelegate Class Implementation The \l{QAbstractItemDelegate::}{paint()} function is - reimplemented from QItemDelegate and is called whenever the view + reimplemented from QStyledItemDelegate and is called whenever the view needs to repaint an item: \snippet itemviews/stardelegate/stardelegate.cpp 0 @@ -93,7 +93,7 @@ The function is invoked once for each item, represented by a QModelIndex object from the model. If the data stored in the item is a \c StarRating, we paint it ourselves; otherwise, we let - QItemDelegate paint it for us. This ensures that the \c + QStyledItemDelegate paint it for us. This ensures that the \c StarDelegate can handle the most common data types. If the item is a \c StarRating, we draw the background if the diff --git a/examples/widgets/itemviews/stardelegate/main.cpp b/examples/widgets/itemviews/stardelegate/main.cpp index 51ca30c73a6..452976bba06 100644 --- a/examples/widgets/itemviews/stardelegate/main.cpp +++ b/examples/widgets/itemviews/stardelegate/main.cpp @@ -101,10 +101,7 @@ int main(int argc, char *argv[]) tableWidget.setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked); tableWidget.setSelectionBehavior(QAbstractItemView::SelectRows); - - QStringList headerLabels; - headerLabels << "Title" << "Genre" << "Artist" << "Rating"; - tableWidget.setHorizontalHeaderLabels(headerLabels); + tableWidget.setHorizontalHeaderLabels({"Title", "Genre", "Artist", "Rating"}); populateTableWidget(&tableWidget); diff --git a/examples/widgets/itemviews/stardelegate/stardelegate.cpp b/examples/widgets/itemviews/stardelegate/stardelegate.cpp index da5902a1608..41ae5a920f2 100644 --- a/examples/widgets/itemviews/stardelegate/stardelegate.cpp +++ b/examples/widgets/itemviews/stardelegate/stardelegate.cpp @@ -65,7 +65,7 @@ void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, painter->fillRect(option.rect, option.palette.highlight()); starRating.paint(painter, option.rect, option.palette, - StarRating::ReadOnly); + StarRating::EditMode::ReadOnly); } else { QStyledItemDelegate::paint(painter, option, index); } @@ -79,9 +79,8 @@ QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option, if (index.data().canConvert()) { StarRating starRating = qvariant_cast(index.data()); return starRating.sizeHint(); - } else { - return QStyledItemDelegate::sizeHint(option, index); } + return QStyledItemDelegate::sizeHint(option, index); } //! [1] @@ -96,9 +95,8 @@ QWidget *StarDelegate::createEditor(QWidget *parent, connect(editor, &StarEditor::editingFinished, this, &StarDelegate::commitAndCloseEditor); return editor; - } else { - return QStyledItemDelegate::createEditor(parent, option, index); } + return QStyledItemDelegate::createEditor(parent, option, index); } //! [2] diff --git a/examples/widgets/itemviews/stardelegate/stardelegate.h b/examples/widgets/itemviews/stardelegate/stardelegate.h index ffc65fbedd9..1fc31f8ee89 100644 --- a/examples/widgets/itemviews/stardelegate/stardelegate.h +++ b/examples/widgets/itemviews/stardelegate/stardelegate.h @@ -57,9 +57,8 @@ class StarDelegate : public QStyledItemDelegate { Q_OBJECT - public: - StarDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {} + using QStyledItemDelegate::QStyledItemDelegate; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; diff --git a/examples/widgets/itemviews/stardelegate/stareditor.cpp b/examples/widgets/itemviews/stardelegate/stareditor.cpp index 19a4b54d9e5..43706eeae0b 100644 --- a/examples/widgets/itemviews/stardelegate/stareditor.cpp +++ b/examples/widgets/itemviews/stardelegate/stareditor.cpp @@ -48,11 +48,11 @@ ** ****************************************************************************/ -#include - #include "stareditor.h" #include "starrating.h" +#include + //! [0] StarEditor::StarEditor(QWidget *parent) : QWidget(parent) @@ -71,35 +71,37 @@ QSize StarEditor::sizeHint() const void StarEditor::paintEvent(QPaintEvent *) { QPainter painter(this); - myStarRating.paint(&painter, rect(), this->palette(), - StarRating::Editable); + myStarRating.paint(&painter, rect(), palette(), + StarRating::EditMode::Editable); } //! [1] //! [2] void StarEditor::mouseMoveEvent(QMouseEvent *event) { - int star = starAtPosition(event->x()); + const int star = starAtPosition(event->x()); if (star != myStarRating.starCount() && star != -1) { myStarRating.setStarCount(star); update(); } + QWidget::mouseMoveEvent(event); } //! [2] //! [3] -void StarEditor::mouseReleaseEvent(QMouseEvent * /* event */) +void StarEditor::mouseReleaseEvent(QMouseEvent *event) { emit editingFinished(); + QWidget::mouseReleaseEvent(event); } //! [3] //! [4] -int StarEditor::starAtPosition(int x) +int StarEditor::starAtPosition(int x) const { - int star = (x / (myStarRating.sizeHint().width() - / myStarRating.maxStarCount())) + 1; + const int star = (x / (myStarRating.sizeHint().width() + / myStarRating.maxStarCount())) + 1; if (star <= 0 || star > myStarRating.maxStarCount()) return -1; diff --git a/examples/widgets/itemviews/stardelegate/stareditor.h b/examples/widgets/itemviews/stardelegate/stareditor.h index 4a4c3a49549..8b1bf2efeda 100644 --- a/examples/widgets/itemviews/stardelegate/stareditor.h +++ b/examples/widgets/itemviews/stardelegate/stareditor.h @@ -59,9 +59,8 @@ class StarEditor : public QWidget { Q_OBJECT - public: - StarEditor(QWidget *parent = 0); + StarEditor(QWidget *parent = nullptr); QSize sizeHint() const override; void setStarRating(const StarRating &starRating) { @@ -78,7 +77,7 @@ protected: void mouseReleaseEvent(QMouseEvent *event) override; private: - int starAtPosition(int x); + int starAtPosition(int x) const; StarRating myStarRating; }; diff --git a/examples/widgets/itemviews/stardelegate/starrating.cpp b/examples/widgets/itemviews/stardelegate/starrating.cpp index 845e474de9f..15e14965e34 100644 --- a/examples/widgets/itemviews/stardelegate/starrating.cpp +++ b/examples/widgets/itemviews/stardelegate/starrating.cpp @@ -48,19 +48,18 @@ ** ****************************************************************************/ +#include "starrating.h" + #include #include -#include "starrating.h" - -const int PaintingScaleFactor = 20; +constexpr int PaintingScaleFactor = 20; //! [0] StarRating::StarRating(int starCount, int maxStarCount) + : myStarCount(starCount), + myMaxStarCount(maxStarCount) { - myStarCount = starCount; - myMaxStarCount = maxStarCount; - starPolygon << QPointF(1.0, 0.5); for (int i = 1; i < 5; ++i) starPolygon << QPointF(0.5 + 0.5 * std::cos(0.8 * i * 3.14), @@ -87,23 +86,19 @@ void StarRating::paint(QPainter *painter, const QRect &rect, painter->setRenderHint(QPainter::Antialiasing, true); painter->setPen(Qt::NoPen); + painter->setBrush(mode == EditMode::Editable ? + palette.highlight() : + palette.foreground()); - if (mode == Editable) { - painter->setBrush(palette.highlight()); - } else { - painter->setBrush(palette.foreground()); - } - - int yOffset = (rect.height() - PaintingScaleFactor) / 2; + const int yOffset = (rect.height() - PaintingScaleFactor) / 2; painter->translate(rect.x(), rect.y() + yOffset); painter->scale(PaintingScaleFactor, PaintingScaleFactor); for (int i = 0; i < myMaxStarCount; ++i) { - if (i < myStarCount) { + if (i < myStarCount) painter->drawPolygon(starPolygon, Qt::WindingFill); - } else if (mode == Editable) { + else if (mode == EditMode::Editable) painter->drawPolygon(diamondPolygon, Qt::WindingFill); - } painter->translate(1.0, 0.0); } diff --git a/examples/widgets/itemviews/stardelegate/starrating.h b/examples/widgets/itemviews/stardelegate/starrating.h index fa773119145..fc3028db58c 100644 --- a/examples/widgets/itemviews/stardelegate/starrating.h +++ b/examples/widgets/itemviews/stardelegate/starrating.h @@ -51,15 +51,15 @@ #ifndef STARRATING_H #define STARRATING_H -#include -#include -#include +#include +#include +#include //! [0] class StarRating { public: - enum EditMode { Editable, ReadOnly }; + enum class EditMode { Editable, ReadOnly }; explicit StarRating(int starCount = 1, int maxStarCount = 5);