利用Qt中的绘图工具绘制一个表,具体效果如下:
#pragma once
#include
#include
#include
#include
class DrawClock : public QWidget
{
Q_OBJECT
public:
explicit DrawClock(QWidget* parent = nullptr);
protected:
void paintEvent(QPaintEvent* pEvent);
private:
void initWin();
void paint(QPainter& painter);
QColor randColor();
private:
QPointF m_hourVec[4];
QPointF m_minVec[4];
QPointF m_secVec[4];
QTimer* m_pTimer;
};
#include "DrawClock.h"
#include
DrawClock::DrawClock(QWidget* parent)
: QWidget(parent)
{
this->initWin();
}
void DrawClock::initWin()
{
m_hourVec[0] = QPointF(4, -5);
m_hourVec[1] = QPointF(0, -45);
m_hourVec[2] = QPointF(-4, -5);
m_hourVec[3] = QPointF(0, 12);
m_minVec[0] = QPointF(3, -5);
m_minVec[1] = QPointF(0, -55);
m_minVec[2] = QPointF(-3, -5);
m_minVec[3] = QPointF(0, 12);
m_secVec[0] = QPointF(2, -5);
m_secVec[1] = QPointF(0, -65);
m_secVec[2] = QPointF(-2, -5);
m_secVec[3] = QPointF(0, 12);
m_pTimer = new QTimer(this);
m_pTimer->start(1000);
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(update()));
}
void DrawClock::paintEvent(QPaintEvent* pEvent)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
int side = qMin(width(), height());
painter.setViewport((width() - side) / 2, (height() - side) / 2, side, side);
painter.setWindow(0, 0, 200, 200);
this->paint(painter);
QWidget::paintEvent(pEvent);
}
void DrawClock::paint(QPainter& painter)
{
QTime time = QTime::currentTime();
painter.translate(QPointF(100, 100));
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(Qt::gray);
painter.setPen(Qt::gray);
painter.save();
painter.drawRect(-30, 30, 60, 10);
painter.restore();
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(randColor(), 1.5, Qt::SolidLine));
painter.save();
painter.drawText(-30, 30, 60, 10, Qt::AlignCenter, QDate::currentDate().toString("yyyy-MM-dd"));
painter.restore();
painter.setBrush(randColor());
painter.setPen(randColor());
painter.save();
painter.rotate(6.0 * time.second());
painter.drawConvexPolygon(m_secVec, 4);
painter.restore();
painter.setBrush(randColor());
painter.setPen(randColor());
painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(m_minVec, 4);
painter.restore();
painter.setBrush(randColor());
painter.setPen(randColor());
painter.save();
painter.rotate(30.0 * (time.hour() + time.minute() / 60.0));
painter.drawConvexPolygon(m_hourVec, 4);
painter.restore();
painter.setBrush(Qt::white);
painter.setPen(Qt::white);
painter.drawEllipse(-1, -1, 3, 3);
QPen pen1(randColor(), 2.5);
QPen pen2(randColor(), 1.5);
QPen fontPen(QColor(qrand() % 255 - 0, qrand() % 255 - 0, qrand() % 255 - 0), 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
for (int i = 1; i <= 60; ++i)
{
painter.save();
painter.rotate(6 * i);
if (i % 5 == 0)
{
painter.setPen(pen1);
painter.drawLine(0, -98, 0, -90);
painter.setPen(fontPen);
painter.drawText(-20, -82, 40, 20,Qt::AlignHCenter | Qt::AlignTop,QString::number(i / 5));
}
else
{
painter.setPen(pen2);
painter.drawLine(0, -98, 0, -94);
}
painter.restore();
}
}
QColor DrawClock::randColor()
{
return QColor(rand() % 255 - 0, rand() % 255 - 0, rand() % 255 - 0);
}