属性(Properties)
· minimum : int
· maximum: int
· value : int
· singleStep : int
· fontSize : int
· family : QString
· color: QColor
· backgroundColor : QColor
· annulusColor : QColor
· annulusBackgroundColor : QColor
截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b12112d81974275c8373d52b8c0bd086.png)
源码
dialwidget.h
#ifndef DIALWIDGET_H
#define DIALWIDGET_H
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
class DialWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(int minimum MEMBER m_Minimum READ minimum WRITE setMinimum)
Q_PROPERTY(int maximum MEMBER m_Maximum READ maximum WRITE setMaximum)
Q_PROPERTY(int value MEMBER m_Value READ value WRITE setValue)
Q_PROPERTY(int singleStep MEMBER m_SingleStep READ singleStep WRITE setSingleStep)
Q_PROPERTY(int fontSize MEMBER m_FontSize READ fontSize WRITE setFontSize)
Q_PROPERTY(QString family MEMBER m_Family READ family WRITE setFamily)
Q_PROPERTY(QColor color MEMBER m_Color READ color WRITE setColor)
Q_PROPERTY(QColor backgroundColor MEMBER m_BackgroundColor READ backgroundColor WRITE setBackgroundColor)
Q_PROPERTY(QColor annulusColor MEMBER m_AnnulusColor READ annulusColor WRITE setAnnulusColor)
Q_PROPERTY(QColor annulusBackgroundColor MEMBER m_AnnulusBackgroundColor
READ annulusBackgroundColor WRITE setAnnulusBackgroundColor)
public:
explicit DialWidget(QWidget *parent = nullptr);
int minimum() const;
void setMinimum(int);
int maximum() const;
void setMaximum(int);
int value() const;
void setValue(int);
int singleStep() const;
void setSingleStep(int);
int fontSize() const;
void setFontSize(int);
QString family() const;
void setFamily(QString);
QColor color() const;
void setColor(QColor);
QColor backgroundColor() const;
void setBackgroundColor(QColor);
QColor annulusColor() const;
void setAnnulusColor(QColor);
QColor annulusBackgroundColor() const;
void setAnnulusBackgroundColor(QColor);
protected:
void drawAnnulus(QPainter &painter, QRect rect, int width);
protected:
void paintEvent(QPaintEvent *event);
void wheelEvent(QWheelEvent *event);
private:
QFont m_Font;
private:
int m_Value;
int m_Maximum;
int m_Minimum;
int m_SingleStep;
int m_FontSize;
QString m_Family;
QColor m_Color;
QColor m_BackgroundColor;
QColor m_AnnulusColor;
QColor m_AnnulusBackgroundColor;
};
#endif
dialwidget.cpp
#include "DialWidget.h"
#include <QDebug>
DialWidget::DialWidget(QWidget *parent) : QWidget(parent)
{
m_Value = 0;
m_SingleStep = 1;
m_Maximum = 100;
m_Minimum = 0;
m_Value = 0;
m_FontSize = 20;
m_Font.setPixelSize(m_FontSize);
m_Font.setFamily(m_Family);
m_AnnulusColor = m_Color = QColor(Qt::green);
m_AnnulusBackgroundColor = QColor(Qt::black);
m_BackgroundColor = QColor(Qt::black);
}
int DialWidget::minimum() const
{
return m_Minimum;
}
void DialWidget::setMinimum(int v)
{
m_Minimum = v;
m_Value = m_Minimum;
}
int DialWidget::maximum() const
{
return m_Maximum;
}
void DialWidget::setMaximum(int v)
{
m_Maximum = v;
}
int DialWidget::value() const
{
return m_Value;
}
void DialWidget::setValue(int v)
{
m_Value = v;
}
int DialWidget::singleStep() const
{
return m_SingleStep;
}
void DialWidget::setSingleStep(int v)
{
m_SingleStep = v;
}
int DialWidget::fontSize() const
{
return m_FontSize;
}
void DialWidget::setFontSize(int v)
{
m_FontSize = v;
m_Font.setPixelSize(m_FontSize);
repaint();
}
QString DialWidget::family() const
{
return m_Family;
}
void DialWidget::setFamily(QString v)
{
m_Family = v;
m_Font.setFamily(m_Family);
repaint();
}
QColor DialWidget::color() const
{
return m_Color;
}
void DialWidget::setColor(QColor v)
{
m_Color = v;
repaint();
}
QColor DialWidget::backgroundColor() const
{
return m_BackgroundColor;
}
void DialWidget::setBackgroundColor(QColor v)
{
m_BackgroundColor = v;
repaint();
}
QColor DialWidget::annulusColor() const
{
return m_AnnulusColor;
}
void DialWidget::setAnnulusColor(QColor v)
{
m_AnnulusColor = v;
repaint();
}
QColor DialWidget::annulusBackgroundColor() const
{
return m_AnnulusBackgroundColor;
}
void DialWidget::setAnnulusBackgroundColor(QColor v)
{
m_AnnulusBackgroundColor = v;
}
void DialWidget::drawAnnulus(QPainter &painter, QRect rect, int width)
{
QPainterPath path, path2, path3;
QRect outRect;
QRect innerRect;
painter.save();
outRect = rect;
innerRect = QRect(rect.x() + width, rect.y() + width,
rect.width() - 2 * width, rect.height() - 2 * width);
painter.fillRect(outRect, m_BackgroundColor);
painter.setPen(Qt::NoPen);
painter.setBrush(m_AnnulusColor);
path.moveTo(outRect.center());
path.arcTo(outRect, -30, 240);
painter.drawPath(path);
path2.moveTo(outRect.center());
path2.arcTo(outRect, -30, 240 - 240 * (m_Value - m_Minimum) / (m_Maximum - m_Minimum));
painter.setBrush(m_AnnulusBackgroundColor);
painter.drawPath(path2);
path3.moveTo(innerRect.center());
path3.arcTo(innerRect, 0, 360);
painter.setBrush(m_BackgroundColor);
painter.drawPath(path3);
painter.restore();
}
void DialWidget::wheelEvent(QWheelEvent *event)
{
QPoint numPixels = event->pixelDelta();
QPoint numDegrees = event->angleDelta() / 8;
if (!numPixels.isNull()) {
m_Value += numPixels.y();
} else if (!numDegrees.isNull()) {
QPoint numSteps = numDegrees / 15;
m_Value += numSteps.y() * m_SingleStep;
if (m_Value > m_Maximum)
m_Value = m_Maximum;
if (m_Value < m_Minimum)
m_Value = m_Minimum;
}
event->accept();
repaint();
}
void DialWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
drawAnnulus(painter, event->rect(), 20);
painter.setFont(m_Font);
painter.setPen(m_Color);
painter.drawText(event->rect(), Qt::AlignCenter, QString::number(m_Value));
}