Qt之实现表针时钟小工具

    xiaoxiao2021-12-14  16

    有时单纯的主界面看起来比较呆板,在上面加入一些动态元素就生动许多。

    实现思路是以QDialog为基类,在主窗口上新建子窗口,让子窗口透明化,在子窗口中绘制表盘、时分秒表针等,主窗口移动时,子窗口也跟着移动。

    一、实现原理:

    利用QTimer计时器,每隔1s触发信号update()进行更新窗口,更新窗口会触发paintEvent(QPaintEvent *),在绘制事件中利用系统时间对各个指针进行刷新。

    二、清单:

    PaintClock.h/PaintClock.cpp :绘制表钟函数,用类实现可降低耦合性

    三、调用

    1. 调用比较简单,在主窗口构造函数中new出即可:

    m_pclock_ = new PaintClock(this);

    m_pclock->show();

    2. 让表钟窗口随主窗口移动而移动,需重载QDialog的event Filter,在主窗口类中添加:

    protected:

    bool eventFilter(QObject *, QEvent *);

    bool MyMainUI::eventFilter(QObject *object, QEvent *event)

    { switch (event->type()) { case QEvent::Resize: { d->pclock_->move(this->geometry().x()+this->frameGeometry().width()/2.5, this->geometry().y()+this->frameGeometry().height()/3); break; } case QEvent::Move: { d->pclock_->move(this->geometry().x()+this->frameGeometry().width()/2.5, this->geometry().y()+this->frameGeometry().height()/3); break; } default: break; } return false; } 四、PaintClock.h/PaintClock.cpp具体实现如下:

    /** \file PaintClock.h \author Jack Li */ #ifndef PAINTCLOCK_H #define PAINTCLOCK_H #include #include class PaintClock : public QDialog { public: PaintClock(QWidget *parent); ~PaintClock(); void show(); void move(int x, int y); protected: void paintEvent(QPaintEvent * event); private: PaintClock(PaintClock const &); void operator=(PaintClock const &); struct Private; Private * const d; }; #endif // PAINTCLOCK_H /** \file PaintClock.cpp \author Jack Li */ #include "PaintClock.h" #include #include #include #include struct PaintClock::Private { Private(PaintClock * parent) {} /// QTimer * timer_; }; PaintClock::PaintClock(QWidget * parent) : QDialog(parent), d(new PaintClock::Private(this)) { /* set initial location */ setGeometry(parent->geometry().x()+parent->frameGeometry().width()/2.5, parent->geometry().y()+parent->frameGeometry().height()/3, 200, 200); /* set windows no frame */ setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint); /* set background translucent */ setWindowOpacity(1); setAttribute(Qt::WA_TranslucentBackground); d->timer_ = new QTimer(this); connect(d->timer_, SIGNAL(timeout()), this, SLOT(update())); d->timer_->start(1000); } PaintClock::~PaintClock() { if (d->timer_) delete d->timer_; delete d; } void PaintClock::move(int x, int y) { QDialog::move(x, y); } void PaintClock::show() { QDialog::show(); } void PaintClock::paintEvent(QPaintEvent *) { static const QPoint hour[3] = { QPoint(7, 8), QPoint(-7, 8), QPoint(0, -40) }; static const QPoint min[3] = { QPoint(7, 8), QPoint(-7, 8), QPoint(0, -60) }; static const QPoint sec[3] = { QPoint(2, 8), QPoint(-2, 8), QPoint(0, -80) }; QTime time = QTime::currentTime(); QPainter paint(this); //painter moves to center paint.translate(100, 100); //paint.drawLine(0, 0, 100, 100); paint.setRenderHint(QPainter::Antialiasing);//set painter antialias paint.setBrush(Qt::red); paint.setPen(Qt::red); paint.save(); paint.rotate(6.0 * time.second()); /* paint sec pointer */ paint.drawConvexPolygon(sec, 4); paint.restore(); paint.setBrush(Qt::blue); paint.setPen(Qt::blue); paint.save(); paint.rotate(6.0 * (time.minute() + time.second() /60.0));/* 6(360 / 60) */ /* paint min pointer */ paint.drawConvexPolygon(min, 4); paint.restore(); paint.setBrush(Qt::black); paint.setPen(Qt::black); paint.save(); paint.rotate(30.0 * (time.hour() + time.minute() / 60.0));/* 30(360 / 12) */ /* paint hour pointer */ paint.drawConvexPolygon(hour, 4); paint.restore(); /* paint clock dial plate */ for (int i = 1; i <= 60; i++) { paint.rotate(6);/* 6° */ if (i == 60) { paint.save(); paint.setBrush(Qt::red); paint.setPen(Qt::red); paint.drawLine(0, -98, 0, -82); /* -98*cos(30).. */ paint.restore(); } else if (i == 30) { paint.save(); paint.setBrush(Qt::red); paint.setPen(Qt::red); paint.drawLine(0, -98, 0, -83); /* -98*cos(30).. */ paint.restore(); } else if (i == 45 || i == 15) { paint.save(); paint.setBrush(Qt::yellow); paint.setPen(Qt::yellow); paint.drawLine(0, -98, 0, -84); /* -98*cos(30).. */ paint.restore(); } else if (i %5 == 0) { paint.save(); paint.setBrush(Qt::yellow); paint.setPen(Qt::yellow); paint.drawLine(0, -98, 0, -86); /* -98*cos(30).. */ paint.restore(); } else { paint.drawLine(0, -98, 0, -88); /* -98*cos(30).. */ } } /* paint number:12,6,9,3 */ paint.save(); paint.setBrush(Qt::black); paint.setPen(Qt::black); QFont* pFont = new QFont("宋体", 11, 75, false); paint.setFont(*pFont); paint.drawText(QRectF(-8, -80, 25, 15), "12"); paint.drawText(QRectF(-4, 70, 25, 15), "6"); paint.drawText(QRectF(-75, -5, 25, 15), "9"); paint.drawText(QRectF(70, -5, 25, 15), "3"); delete pFont; paint.restore(); }

    五、效果图:

    转载请注明原文地址: https://ju.6miu.com/read-962422.html

    最新回复(0)