I use the rotate
method to draw the circle, but this creates gradual irregularities (visible on the outside of the circle).
Furthermore, I can only get this result if I iterate ten times the hue value in the for loop. If I use the wheel for other purposes (e.g., dynamically changing it), the recalculation would require a lot of computing power, which will lead to performance problems.
I can use QPainter::SmoothPixelTransformation
to get a slightly better result, but the problem is still that the circle is drawn messily. That's not what I want.
The question is therefore whether I should use QPainter::rotate()
here, or should I look for other methods? If so, I need an example.
.h
#include <QWidget>class ColorWheel : public QWidget{ Q_OBJECTpublic: ColorWheel(QWidget *parent = nullptr); ~ColorWheel(); static QPixmap wheel(int width, int height);signals: // QWidget interfaceprotected: virtual void paintEvent(QPaintEvent *) override;private: QPixmap m_wheel; QRect m_wheelRect;};
.cpp
#include "colorwheel.h"#include <QPainter>ColorWheel::ColorWheel(QWidget *parent) : QWidget(parent){ setFixedSize(400,400); m_wheelRect = rect().marginsRemoved(QMargins(18,18,18,18)); m_wheel = wheel(width(),height());}ColorWheel::~ColorWheel(){}void ColorWheel::paintEvent(QPaintEvent *){ QPainter painter(this); // painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform); painter.setPen(QPen(Qt::black, 3)); // painter.drawEllipse(m_wheelRect); painter.drawPixmap(m_wheelRect, m_wheel);}QPixmap ColorWheel::wheel(int width, int height){ // |- {1} init wheels pixmap with wheelrect size and fill transperent QPixmap wheel = QPixmap(width, height); wheel.fill(Qt::transparent); // |- {2} paint on the pixmap QPainter painter(&wheel); painter.setRenderHint(QPainter::Antialiasing); painter.translate(width/2,height/2); // |- {3} draw filled rects qreal pxWidth = (((qreal)width) / 257) / 2.0; qreal steps = 0; for (int hue = 0; hue <= 3590; ++hue) { painter.rotate(360.0/3590.0); for (int saturation = 0; saturation <= 255; ++saturation, steps+=pxWidth) { QColor clr = QColor::fromHsv(hue/10,saturation, 255); painter.fillRect(QRectF(steps, 0, pxWidth, 1.0),clr); } steps = 0; } return wheel;}