summaryrefslogtreecommitdiffstats
path: root/examples/widgets/itemviews/stardelegate/starrating.cpp
diff options
context:
space:
mode:
authorChristian Ehrlicher <ch.ehrlicher@gmx.de>2018-11-22 21:21:25 +0100
committerChristian Ehrlicher <ch.ehrlicher@gmx.de>2018-12-04 07:00:44 +0000
commit1cfe064632099e036f964832c23afe266e54d59e (patch)
treeda3fe64e73993b8df89547f7db1fa53625ae04f8 /examples/widgets/itemviews/stardelegate/starrating.cpp
parent51f6d5d8c261fb94adc9bf5754cabf8e37236af5 (diff)
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 <szehowe.koh@gmail.com> Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
Diffstat (limited to 'examples/widgets/itemviews/stardelegate/starrating.cpp')
-rw-r--r--examples/widgets/itemviews/stardelegate/starrating.cpp27
1 files changed, 11 insertions, 16 deletions
diff --git a/examples/widgets/itemviews/stardelegate/starrating.cpp b/examples/widgets/itemviews/stardelegate/starrating.cpp
index 845e474de9..15e14965e3 100644
--- a/examples/widgets/itemviews/stardelegate/starrating.cpp
+++ b/examples/widgets/itemviews/stardelegate/starrating.cpp
@@ -48,19 +48,18 @@
**
****************************************************************************/
+#include "starrating.h"
+
#include <QtWidgets>
#include <cmath>
-#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);
}