QT----电子时钟
来源:互联网 发布:知乎 低价服务器 编辑:程序博客网 时间:2024/09/21 08:57
2011-08-01 08:28 1614人阅读 评论(0)收藏 举报
源文件:
拖动的位置减去上一次位置就计算出来左上角应该移动的位置差。
中间的冒号是一秒闪烁一次
新建一个继承自QLCDNumber的类
头文件:
- #define DIGICLOCK_H
- #include <QLCDNumber>
- class DIgiClock : public QLCDNumber
- {
- Q_OBJECT
- public:
- DIgiClock(QWidget *parent = 0);
- void mousePressEvent(QMouseEvent *);//重载函数响应鼠标按下
- void mouseMoveEvent(QMouseEvent *);//重载函数响应鼠标移动
- public slots:
- void ShowTime();
- private:
- QPoint dragPosition;
- bool showColon;
- };
- #endif // DIGICLOCK_H
#ifndef DIGICLOCK_H#define DIGICLOCK_H#include <QLCDNumber>class DIgiClock : public QLCDNumber{ Q_OBJECTpublic: DIgiClock(QWidget *parent = 0); void mousePressEvent(QMouseEvent *);//重载函数响应鼠标按下 void mouseMoveEvent(QMouseEvent *);//重载函数响应鼠标移动public slots: void ShowTime();private: QPoint dragPosition; bool showColon;};#endif // DIGICLOCK_H
源文件:
- #include <QTime>
- #include <QTimer>
- #include <QMouseEvent>
- DIgiClock::DIgiClock(QWidget *parent) :
- QLCDNumber(parent)
- {
- QPalette p=palette();
- p.setColor(QPalette::Window,Qt::blue);
- setPalette(p);
- setWindowFlags(Qt::FramelessWindowHint);
- setWindowOpacity(0.5);
- QTimer *timer=new QTimer(this);
- connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime()));
- timer->start(500);
- ShowTime();
- resize(150,60);
- showColon=true;
- }
- void DIgiClock::ShowTime()
- {
- QTime time=QTime::currentTime();
- QString text=time.toString("hh:mm");
- if(showColon)
- {
- text[2]=':';
- showColon=false;
- }
- else
- {
- text[2]=' ';
- showColon=true;
- }
- display(text);
- }
- void DIgiClock::mousePressEvent(QMouseEvent *event)
- {
- if(event->button()==Qt::LeftButton)
- {
- dragPosition=event->globalPos()-frameGeometry().topLeft();
- event->accept();
- }
- else if(event->button()==Qt::RightButton)
- {
- close();
- }
- }
- void DIgiClock::mouseMoveEvent(QMouseEvent *event)
- {
- move(event->globalPos()-dragPosition);
- event->accept();
- }
#include "digiclock.h"#include <QTime>#include <QTimer>#include <QMouseEvent>DIgiClock::DIgiClock(QWidget *parent) : QLCDNumber(parent){ QPalette p=palette(); p.setColor(QPalette::Window,Qt::blue); setPalette(p); setWindowFlags(Qt::FramelessWindowHint); setWindowOpacity(0.5); QTimer *timer=new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime())); timer->start(500); ShowTime(); resize(150,60); showColon=true;}void DIgiClock::ShowTime(){ QTime time=QTime::currentTime(); QString text=time.toString("hh:mm"); if(showColon) { text[2]=':'; showColon=false; } else { text[2]=' '; showColon=true; } display(text);}void DIgiClock::mousePressEvent(QMouseEvent *event){ if(event->button()==Qt::LeftButton) { dragPosition=event->globalPos()-frameGeometry().topLeft(); event->accept(); } else if(event->button()==Qt::RightButton) { close(); }}void DIgiClock::mouseMoveEvent(QMouseEvent *event){ move(event->globalPos()-dragPosition); event->accept();}
上述代码主要有三点:
1,显示时间:
- connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime()));
- timer->start(500);
QTimer *timer=new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime())); timer->start(500);每隔500毫秒调用一下ShowTime()槽函数。
- {
- QTime time=QTime::currentTime();
- QString text=time.toString("hh:mm");
- if(showColon)
- {
- text[2]=':';
- showColon=false;
- }
- else
- {
- text[2]=' ';
- showColon=true;
- }
- display(text);
- }
void DIgiClock::ShowTime(){ QTime time=QTime::currentTime(); QString text=time.toString("hh:mm"); if(showColon) { text[2]=':'; showColon=false; } else { text[2]=' '; showColon=true; } display(text);}获取当前时间,然后放到QString中,然后调用父类方法display出来,这里的QString就有限制了,只能是QLCDNumber能显示的内容才行。
其中中间冒号间歇出现。
2,响应鼠标按下
- void DIgiClock::mousePressEvent(QMouseEvent *event)
- {
- if(event->button()==Qt::LeftButton)
- {
- dragPosition=event->globalPos()-frameGeometry().topLeft();
- event->accept();
- }
- else if(event->button()==Qt::RightButton)
- {
- close();
- }
- }
void DIgiClock::mousePressEvent(QMouseEvent *event){ if(event->button()==Qt::LeftButton) { dragPosition=event->globalPos()-frameGeometry().topLeft(); event->accept(); } else if(event->button()==Qt::RightButton) { close(); }}
左键记录按下的位置,右键关闭
3,响应鼠标拖动
- {
- move(event->globalPos()-dragPosition);
- event->accept();
- }
void DIgiClock::mouseMoveEvent(QMouseEvent *event){ move(event->globalPos()-dragPosition); event->accept();}利用move函数将程序移动到指定位置,由于是窗口左上角移动到什么地方,所以这里做了计算。
计算内容应该是拖动的位置减去上一次位置的差加上左上角位置:
frameGeometry().topLeft()+【event->globalPos(当前位置)-event->globalPos(按下时候)】
拖动的位置减去上一次位置就计算出来左上角应该移动的位置差。
- QT----电子时钟
- Qt:电子时钟
- QT:电子时钟
- Qt - 与众不同的电子时钟
- Qt浅谈之电子时钟
- QT时间触发电子时钟
- Qt学习——电子时钟
- Qt学习——电子时钟 .
- Qt浅谈之四:电子时钟
- Qt学习(五)-----自定义电子时钟
- Qt学习13——电子时钟
- 电子时钟
- 电子时钟
- Qt之对话框设计——电子时钟
- js液晶电子时钟
- js普通电子时钟
- LCD电子时钟
- 电子时钟js代码
- www.ecosway.com-- Problem A
- parse命令行的函数简介:getopt和getopt_long (x264中用到了这两个函数)
- 十个经过验证的可用性技巧
- Oracle中5个核心Sql语句的基本构造:Select、Insert、Update、Delete和Merge
- OI 用 Emacs 配置文件
- QT----电子时钟
- android绘制view的过程(自定义view一)
- 2010年机器视觉试题及答案(山东大学控制科学与工程学院)
- OSG+VS2010+windowsxp安装和编译
- 【BZOJ1002】【高精度】【找规律】轮状病毒
- C++之旅
- 多进程同时访问文件并发问题解决方法
- Matlab Skeleton 骨骼化 参数说明
- 【Android游戏开发之三】详细剖析 SurfaceView ! Callback以及SurfaceHolder!!