QT时钟

在widget.h中

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMouseEvent>
#include <QToolTip>
#include <QDebug>
#include <QMenu>
#include <QPainter>
#include <QTime>
#include <QTimer>
#include <QSystemTrayIcon>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected:
    virtual void mousePressEvent(QMouseEvent *event);   //虚函数重写
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mouseDoubleClickEvent(QMouseEvent *event);
    virtual void contextMenuEvent(QContextMenuEvent *event);      //右键菜单事件
    virtual void paintEvent(QPaintEvent *event);    

private:
    void createRightPopActions();

private:
    Ui::Widget *ui;
    QPoint mouseWindowTopLeft; //鼠标相对窗口左上角的坐标         在mousePressEvent 得到
    QPoint mouseDeskTopLeft;   //鼠标相对于桌面左上角坐标         在mouseMoveEvent实时获取
    QPoint windowDeskTopLeft;  //窗口左上角相对于桌面左上角坐标    在mouseMoveEvent实时计算(矢量)获得

    QMenu *popMenu;               //右键弹出式菜单
    QAction *adjustTimeAction;    //右键弹出式菜单中的内容
    QAction *adjustDateAction;
    QAction *quitAction;
    QTimer *rtTimer;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    rtTimer = new QTimer;

    setWindowFlags(Qt::FramelessWindowHint | windowFlags()); //不要标题栏 不影响之前的flags
    setMouseTracking(true);       //鼠标跟踪
    createRightPopActions();       

    connect(rtTimer, SIGNAL(timeout()),
            this, SLOT(update()));           //rtTimer超时后会直接调用paintEvent进行重绘
    rtTimer->start(1000);
    // 两个相等
//    rtTimer->setInterval(1000);
//    rtTimer->start();
}

Widget::~Widget()
{
    delete ui;
}

/* 创建右键菜单 */
void Widget::createRightPopActions()
{
    popMenu = new QMenu(this);
    popMenu->setStyleSheet("background-color: rgb(30,144,255);");

    adjustTimeAction = new QAction(this);
    adjustTimeAction->setText("修改时间"); //gbk -> Unicode

    adjustDateAction = new QAction(this);
    adjustDateAction->setText("修改日期");

    quitAction = new QAction(this);
    quitAction->setText("退出");

    connect(quitAction, SIGNAL(triggered(bool)),
            this, SLOT(close()));                  //关联退出的Action
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    QString posStr = QString("%1, %2").arg(event->pos().x()).arg(event->pos().y());

    QToolTip::showText(QCursor::pos(), posStr, this); //QCursor::pos()光标所在的坐标

    if(event->buttons() & Qt::LeftButton)
    {
        mouseDeskTopLeft = event->globalPos();
        windowDeskTopLeft = mouseDeskTopLeft - mouseWindowTopLeft;  //获取在该软件的坐标
        this->move(windowDeskTopLeft);
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    mouseWindowTopLeft = event->pos();   //按下获得鼠标位置,
}

void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if(this->isFullScreen())
    {
        this->showNormal();
    }
    else
    {
        this->showFullScreen();
    }
}

void Widget::contextMenuEvent(QContextMenuEvent *event)
{
    qDebug() << "contextMenuEvent ing ...";

    popMenu->clear();
    popMenu->addAction(adjustTimeAction);
    popMenu->addAction(adjustDateAction);
    popMenu->addSeparator();    //实线分割
    popMenu->addAction(quitAction);

    //popMenu->exec(); //堵住
    popMenu->exec(QCursor::pos()); // //菜单出现的位置为当前鼠标的位置
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    // 时针、分针、秒针位置 - 多边形
    static const QPoint hourHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -30)
    };
    static const QPoint minuteHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -65)
    };

    static const QPoint secondHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -80)
    };


    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.translate(this->width() / 2, this->height() / 2);
    painter.setPen(Qt::white);
    //painter.drawLine(QPoint(88, 0), QPoint(96, 0));

    int side = qMin(width(), height()); //定义一个边界,保证是圆形的
    painter.scale(side / 200.0, side / 200.0);

    {//绘制表盘
        //绘制小时线
        for(int i = 0; i < 12; ++i)
        {
            painter.drawLine(QPoint(88, 0), QPoint(96, 0));
            painter.rotate(30);
        }

        //painter.drawLine(92, 0, 96, 0);                  //先画一根分钟

        // 绘制分钟线 (360度 / 60 = 6度)
        for (int j = 0; j < 60; ++j) {
            if ((j % 5) != 0)                             //碰到小时就不画
                painter.drawLine(92, 0, 96, 0);
            painter.rotate(6.0);
        }
    }

    {//绘制指针


        QTime time = QTime::currentTime();  //获取当前时间

        painter.save();
        painter.setPen(Qt::NoPen);  //默认情况下时有一个像素的边框,所以我这里用NoPen = 不要边框
        painter.setBrush(Qt::white);
        painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))); //时钟不一定对准时钟线 需要考虑分的感受
        painter.drawConvexPolygon(hourHand, 3);
        painter.restore();                //还原画笔

        painter.save();
        painter.setPen(Qt::NoPen);
        painter.setBrush(QColor(130, 130, 130));
        painter.rotate(6.0 * (time.minute() + time.second() / 60.0)); //考虑秒
        painter.drawConvexPolygon(minuteHand, 3);
        painter.restore();                //还原画笔


        painter.save();
        painter.setPen(Qt::NoPen);
        painter.setBrush(QColor(50, 50, 50));
        painter.rotate(6.0 * time.second());
        painter.drawConvexPolygon(secondHand, 3);
        painter.restore();                //还原画笔
    }
}


 

painter.save();
painter.restore();总是成对出现。

QTime time = QTime::currentTime(); //获取当前时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风赤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值