基于Qt5的闹钟实现代码
闹钟 源代码
99. P2 = 7;
100. Delay(2);
101.}
102./********************************************************************
143. s = 0;
144. break;
145. default:
146. hour = h;
40.void Time0_Init()
41.{
42. TMOD = 0x01;
43. IE = 0x82;
44. TH0 = 0x4c;
45. TL0 = 0x0 Delay(60);
160.}
161./********************************************************************
162.* 名称 : SetClock()
163.* 功能 : 设置闹钟时间
164.* 输入 : 无
17.sbit KEY1 = P3^2;
18.sbit KEY2 = P3^3;
19.sbit KEY3 = P3^4;
20.sbit BELL = P1^2;
21.
22./********************************************************************
165.* 输出 : 无
166.***********************************************************************/
PyQt5QThread倒计时功能的实现代码
PyQt5QThread倒计时功能的实现代码1.创建多线程类全局变量 secclass work_thread(QThread):timer = pyqtSignal() # 每隔⼀秒发送信号end = pyqtSignal() # 计数完成发送信号def run(self) -> None:while True:self.sleep(1)if sec == 0:self.end.emit() # 发送end信号breakself.timer.emit()2.实例化线程类,绑定计数,技术结束事件bel.setText("20")global secsec = 20self.work_thread = work_thread()self.work_thread.timer.connect(self.count_time)self.work_thread.end.connect(self.end)3.绑定开始按钮事件self.pushButton.clicked.connect(self.start)4.事件⽅法def count_time(self):global secsec = int(bel.text())sec -= 1bel.setText(str(sec))def end(self):self.statusbar.showMessage("计数停⽌")def start(self):self.work_thread.start() # 启动线程PS:知识点扩展PyQt5 倒计时按钮功能实现"""这个例⼦中我们实现了两个功能:菜单按钮、带倒计时的按钮(账户注册的时候经常会碰到)。
"""from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QMenufrom PyQt5.QtCore import QTimerimport sysclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.resize(400,300)self.setWindowTitle('早点毕业吧--按钮(QPushButton)')bt1 = QPushButton("这是什么",self)bt1.move(50,50)self.bt2 = QPushButton('发送验证码',self)self.bt2.move(200,50)"""设置菜单按钮其实很简单,⾸先我们新建⼀个QMenu对象。
QT时钟程序
///*****************.pro文件SOURCES += \analogclock.cppHEADERS += \clock.hQT +=qt3support//************************************ ***analogclock.cpp***************#includ e <qdatetime.h>#include <qpainter.h>#include <qapplication.h>#include <qlabel.h>#include <qwidget.h>#include<qpushbutton.h>#include<qcombobox.h>#include<qmovie.h>#include <qcolor.h>#include "clock.h"static QPoint sed[4]={QPoint(0,-70),QPoint(2,0),QPoint(0,1 0),QPoint(-2,0)};static QPoint min[4]={QPoint(0,-60),QPoint(4,0),QPoint(0,1 2),QPoint(-4,0)};static QPoint hour[4]={QPoint(0,-50),QPoint(6,0),QPoint(0, 13),QPoint(-6,0)};MyTime *mytime=new MyTime();QColor hcolor=QColor(255,0,0);QColor mcolor=QColor(0,255,0);QColor scolor=QColor(0,0,0);QColor hcellcolor=QColor(255,0,0);QColor mcellcolor=QColor(0,0,255);//构造函数MyTime::MyTime(){hour=0; minute=0;second=0;}//getcolorQColor getColor();AnalogClock::AnalogClock( QWidget *parent, const char *name ): QWidget(parent,name ){ QTime curtime0 = QTime::currentTime();//初始启动似的当前时间QDate curdate0 = QDate::currentDate();//初始日期startTimer(1000);//每秒刷新一次setMinimumSize(500,500); setMaximumSize(500,500);setCaption("Good Clock");setGeometry(0,0,0,0);//area显示area = new QLabel("choose area:",this);area->setGeometry(0,0,85,30);//设置地区选择下拉框setArea=new QComboBox(FALSE,this); Q_CHECK_PTR(setArea);setArea->setGeometry(0,30,85,30);setArea->insertItem(" Beijing ",0);setArea->insertItem(" Pairs",1);//-7 setArea->insertItem(" Athens",2);//-6 setArea->insertItem(" Moscow",3);//-5 setArea->insertItem(" Tokyo",4);//+1setArea->insertItem(" Sydny",5);//+1.5 setArea->insertItem(" Hawaii",6);//+6 setArea->insertItem(" Los Angeles",7);//+8//时间默认值mytime->setHour(curtime0.hour());mytime->setMinute(curtime0.minute()); mytime->setSecond(curtime0.second());//change color显示color = new QLabel("change color:",this); color->setGeometry(385,0,90,30);//变颜色按钮h=new QPushButton("H",this);h->setGeometry(385,30,30,30);m=new QPushButton("M",this);m->setGeometry(415,30,30,30);s=new QPushButton("S",this);s->setGeometry(445,30,30,30);//copyright显示right = new QLabel("copyright@20110624 Youngshare",this);right->setGeometry(130,485,500,15);//信号连接connect( setArea, SIGNAL( activated ( int )),this,SLOT( changeArea(int)));connect( h, SIGNAL( clicked()),this,SLOT( changeHourColor ()));connect( m, SIGNAL( clicked()),this,SLOT( changeMinuteCo lor()));connect( s, SIGNAL( clicked()),this,SLOT( changeSecondCo lor()));}//时间分析处理void AnalogClock::changeArea(int areaChoice) { QTime curtime = QTime::currentTime();//没一次刷新要重新获得一下当前时间switch(areaChoice){case 0:mytime->setHour(curtime.hour());mytime->setSecond(curtime.second());mytime->setMinute(curtime.minute());break;case 1:mytime->setHour(curtime.hour()-7);mytime->setMinute(curtime.minute());mytime->setSecond(curtime.second());break;case 2:mytime->setHour(curtime.hour()-6);mytime->setMinute(curtime.minute());mytime->setSecond(curtime.second());break;case 3:mytime->setHour(curtime.hour()-5);mytime->setMinute(curtime.minute());mytime->setSecond(curtime.second());break;case 4:mytime->setHour(curtime.hour()+1); mytime->setMinute(curtime.minute());mytime->setSecond(curtime.second());break;case 5:mytime->setHour(curtime.hour()+1); mytime->setMinute(curtime.minute()+30);mytime->setSecond(curtime.second());break;case 6:mytime->setHour(curtime.hour()+6); mytime->setMinute(curtime.minute());mytime->setSecond(curtime.second());break;case 7:mytime->setHour(curtime.hour()+8); mytime->setMinute(curtime.minute());mytime->setSecond(curtime.second());break;}}//颜色变化处理void AnalogClock::changeHourColor(){hcolor = getColor();}void AnalogClock::changeMinuteColor(){mcolor = getColor();}void AnalogClock::changeSecondColor(){scolor = getColor();}//每秒刷新响应void AnalogClock::timerEvent( QTimerEvent * ){update();}void AnalogClock::paintEvent( QPaintEvent * ) {//QCOORD hourHand[8] = { 2, 0, 0, 2, -2, 0, 0, -25 };//QCOORD minuteHand[8] = { 1, 0, 0, 1, -1, 0, 0, -35 };if(mytime->second>59)mytime->second=mytime->second%60; //进一分钟if(mytime->second==0)mytime->minute++;if(mytime->minute>59){mytime->minute=mytime->minute%60;if(mytime->minute==0)mytime->hour++;}if(mytime->hour>11)mytime->hour=mytime->hour%12; mytime->second++;QPainter painter( this );//painter的起事位置为水平向左rotate旋转是逆时针方向painter.setWindow( -50, -50, 100, 100 );// painter.setBrush( green );painter.save();painter.rotate( 30*(mytime->hour%12) + mytime->minute*0.5);painter.setBrush( hcolor );painter.drawConvexPolygon(sed,4);//painter.drawConvexPolygon( hourHand,4);//画时针QPointArray(painter.restore();painter.save();painter.rotate(mytime->minute*6+0.1*(myti me->second-15));painter.setBrush(mcolor);//painter.drawConvexPolygon( minuteHand,4) ;//画minuteQPointArray(painter.drawConvexPolygon(min,4);painter.restore();painter.save();//画secondpainter.rotate(6 * (mytime->second-15));painter.setPen(scolor);painter.drawLine(0,0,40,0);painter.restore();painter.setPen( hcellcolor );for ( int i = 0; i < 12; i++ ){painter.rotate( 30);painter.drawLine( 42, 0, 46, 0 );}painter.setPen(mcellcolor);for (int j = 0; j < 60; j++){if ((j % 5) != 0)painter.drawLine(42, 0, 44, 0);painter.rotate(6.0);}painter.rotate(72);}//主函数int main( int argc, char **argv ){QApplication app( argc, argv );//setTime();AnalogClock *clock = new AnalogClock; app.setMainWidget( clock );clock->show();return app.exec();}//随即改变颜色QColor getColor(){static int i=0;//颜色选择QColor tempt;i++;if(i>5) i=i%5;switch(i){case 1:tempt=QColor(255,0,0);//红break;case 2:tempt=QColor(0,255,0);//绿break;case 3:tempt=QColor(0,0,255);//蓝break;case 4:tempt=QColor(0,0,0);//黑break;case 5:tempt=QColor(255,255,0);//黄break;}return tempt;}//**********************clock.h********************************#ifndef CLOCK_H#define CLOCK_H#endif // CLOCK_H#include <qdatetime.h>#include <qpainter.h>#include <qapplication.h>#include <qlabel.h>#include <qwidget.h>#include<qpushbutton.h>#include<qcombobox.h>//钟表窗口类class AnalogClock : public QWidget{Q_OBJECTpublic:AnalogClock( QWidget *parent = 0, const char *name = 0 );public slots://设置地区void changeArea(int a);//设置颜色void changeHourColor();void changeMinuteColor();void changeSecondColor();protected:virtual void timerEvent( QTimerEvent *event );virtual void paintEvent( QPaintEvent *ev。
实时电子闹钟52单片机源代码
实时电子闹钟功能:可以在液晶上显示时间星期日期,并按秒实时更新,具有闹钟设定及到时闹钟报警提示功能,闹钟响铃时按任意键可以取消响铃,拓展了四个按键,用于功能选择,数值增大或减小及闹钟查看,每次按键均会有短“滴”声,断电时间不停,再次上电仍将准确显示在液晶上照片描述:用开发板及自己焊接的部分电路板做的实时电子闹钟照片描述:用开发板及自己焊接的部分电路板做的实时电子闹钟照片描述:用开发板及自己焊接的部分电路板做的实时电子闹钟照片描述:用开发板及自己焊接的部分电路板做的实时电子闹钟照片描述:用开发板及自己焊接的部分电路板做的实时电子闹钟照片描述:用开发板及自己焊接的部分电路板做的实时电子闹钟照片描述:用开发板及自己焊接的部分电路板做的实时电子闹钟照片描述:用开发板及自己焊接的部分电路板做的实时电子闹钟实时电子闹钟程序#include <reg52.h>#define uchar unsigned char#define uint unsigned int//按键sbit s1=P3^4; //设置sbit s2=P3^5; //减sbit s3=P3^6; //增sbit s4=P3^7; //闹钟sbit beep=P2^4; //蜂鸣器//1602位声明(实验板)sbit rs1602=P2^5;sbit rw1602=P2^6;sbit en1602=P2^7;/*uchar code table0[9]={"2010-4-13"};uchar code table1[8]={"11:03:05"};*///定义全局变量uchar shi,fen,miao,nian,yue,ri,week,s1num,num,flag,flag_di,flag1,amiao,afen,ashi;//延时x*1msvoid delay(uint x){uint a,b;for(a=0;a<x;a++)for(b=0;b<63;b++);}//转换函数uchar Convert(uchar In_Date){uchar i,Out_Date=0,temp=0;for(i=0;i<8;i++){temp=(In_Date>>i)&0x01;Out_Date|=(temp<<(7-i));}return Out_Date;}//1602写操作//写指令void write1602com(uchar com){rs1602=0;rw1602=0;en1602=0;P0=0xff;P0=Convert(com);delay(2);en1602=1;delay(2);en1602=0;delay(2);rs1602=1;}//写数据void write1602data(uchar adata) {rs1602=1;rw1602=0;en1602=0;P0=0xff;P0=Convert(adata);delay(2);en1602=1;delay(2);en1602=0;delay(2);rs1602=0;}//1602初始化设置void init_1602(){rs1602=0;rw1602=0;en1602=0;write1602com(0x38);write1602com(0x0c);write1602com(0x06);write1602com(0x01);write1602com(0x80);//外部中断EA=1;EX0=1;IT0=1;//闹钟标志位flag1=0;flag=0;flag_di=1;s1num=0;}//在1602中显示时分秒void lcd_sfm(uchar add,uchar adata){uchar shiwei,gewei;shiwei=adata/10;gewei=adata%10;write1602com(0x80+0x40+add); //在高位停留// delay(10);write1602data(shiwei+0x30);write1602data(gewei+0x30);}//在1602中显示年月日void lcd_nyr(uchar add,uchar adata){uchar shiwei,gewei;shiwei=adata/10;gewei=adata%10;write1602com(0x80+add); //在高位停留// delay(10);write1602data(shiwei+0x30);write1602data(gewei+0x30);}//1602时钟横杠等字符的显示void lcd1602showsign(){lcd_nyr(0,20);write1602com(0x80+4);write1602data('-');write1602com(0x80+7);write1602data('-');write1602com(0x80+0x40+2);write1602data(':');write1602com(0x80+0x40+5);write1602data(':');}//DS12887部分程序//位申明sbit ds12887_ds=P2^3;sbit ds12887_rw=P2^2;sbit ds12887_as=P2^1;sbit ds12887_cs=P2^0;sbit ds12887_irq=P3^2;//ds12887写数据及指令操作void ds12887_write(uchar add,uchar adata) {ds12887_as=0;ds12887_ds=1;ds12887_rw=1;ds12887_cs=0;ds12887_as=1;P1=add;ds12887_as=0;ds12887_rw=0;P1=adata;ds12887_rw=1;ds12887_as=1;}//ds12887读数据操作uchar ds12887_read(uchar add){uchar k;ds12887_ds=1;ds12887_rw=1;ds12887_as=1;ds12887_cs=0;P1=add;ds12887_as=0;ds12887_ds=0;P1=0xff;k=P1;ds12887_ds=1;ds12887_cs=1;ds12887_as=1;return k;}//设置ds12887初始时间void set_time(){ds12887_write(0x00,50); //secondds12887_write(0x02,59); //minuteds12887_write(0x04,23); //hourds12887_write(0x06,3); //dayds12887_write(0x07,16); //dateds12887_write(0x08,4); //monthds12887_write(0x09,10); //year}//闹钟滴滴响声void didi(){beep=0;delay(100);beep=1;delay(100);beep=0;delay(100);beep=1;}//ds12887初始化void init_ds12887(){ds12887_write(0x0a,0x20);ds12887_write(0x0B,0x26);//SET置1还是置0??set_time();}//闹钟设定void set_alarm(uchar hour,uchar minute,uchar second) {ds12887_write(1,second);ds12887_write(3,minute);ds12887_write(5,hour);}//读取闹钟数据void read_alarm(){amiao=ds12887_read(1);afen=ds12887_read(3);ashi=ds12887_read(5);}//显示星期void show_week(){switch(week){case 1:write1602com(0x80+11);write1602data('M');write1602data('O');write1602data('N');break;case 2:write1602com(0x80+11);write1602data('T');write1602data('U');write1602data('S');break;case 3:write1602com(0x80+11);write1602data('W');write1602data('E');write1602data('N');break;case 4:write1602com(0x80+11);write1602data('T');write1602data('H');write1602data('U');break;case 5:write1602com(0x80+11);write1602data('F');write1602data('R');write1602data('I');break;case 6:write1602com(0x80+11);write1602data('S');write1602data('A');write1602data('T');break;case 7:write1602com(0x80+11);write1602data('S');write1602data('U');write1602data('N');break;}}//清除设置星期时的数字型显示void clearnumofweek(){write1602com(0x80+14);write1602data(' ');write1602data(' ');}//按键扫描void keyscan(){if(flag_di==0){if((s1==0)||(s2==0)||(s3==0)||(s4==0)) {delay(5);if((s1==0)||(s2==0)||(s3==0)||(s4==0)) {while(!(s1&&s2&&s3&&s4));didi();flag_di=1;}}// flag_di=1;}if(s1==0){delay(10);if(s1==0){s1num++;if(flag1==1){if(s1num==4){s1num=1;// write1602com(0x80+0x40+7);// write1602com(0x0f);}}// s1num++;flag=1;while(!s1);beep=0;delay(50);beep=1;switch(s1num){case 1:write1602com(0x80+0x40+7);write1602com(0x0f);break;case 2:write1602com(0x80+0x40+4);write1602com(0x0f);break;case 3:write1602com(0x80+0x40+1);write1602com(0x0f);break;case 4:write1602com(0x80+15);write1602com(0x0f);break;case 5:write1602com(0x80+9);write1602com(0x0f);break;case 6:write1602com(0x80+6);write1602com(0x0f);break;case 7:write1602com(0x80+3);write1602com(0x0f);break;case 8:ds12887_write(0x00,miao); //secondds12887_write(0x02,fen); //minuteds12887_write(0x04,shi); //hourds12887_write(0x06,week); //dayds12887_write(0x07,ri); //dateds12887_write(0x08,yue); //monthds12887_write(0x09,nian); //year// delay(100);flag=0;s1num=0;break;}}if(s1num!=0){if(s2==0){delay(5);if(s2==0){while(!s2);beep=0;delay(50);beep=1;switch(s1num){case 1:miao++;if(miao==60)miao=0;lcd_sfm(6,miao);write1602com(0 x80+0x40+7);break;case 2:fen++;if(fen==60)fen=0;lcd_sfm(3,fen);write1602com(0 x80+0x40+4);break;case 3:shi++;if(shi==24)shi=0;lcd_sfm(0,shi);write1602com(0 x80+0x40+1);break;case 4:week++;if(week==8)week=1;lcd_nyr(14,week);write1602com(0 x80+15);break;case 5:ri++;if(ri==32)ri=1;lcd_nyr(8,ri);write1602com(0break;case 6:yue++;if(yue==13)yue=1;lcd_nyr(5,yue);write1602com(0 x80+6);break;case 7:nian++;if(nian==100)nian=0;lcd_nyr(2,nian);write1602com(0 x80+3);break;}}}if(s3==0){delay(5);if(s3==0){while(!s3);beep=0;delay(50);beep=1;switch(s1num){case 1:miao--;if(miao==-1)miao=59;lcd_sfm(6,miao);write1602com(0 x80+0x40+7);break;case 2:fen--;if(fen==-1)fen=59;lcd_sfm(3,fen);write1602com(0 x80+0x40+4);break;case 3:shi--;if(shi==-1)shi=23;lcd_sfm(0,shi);write1602com(0 x80+0x40+1);break;case 4:week--;if(week==0)week=7;lcd_nyr(14,week);write1602com(0 x80+15);break;case 5:ri--;if(ri==0)ri=31;lcd_nyr(8,ri);write1602com(0 x80+9);break;case 6:yue--;if(yue==0)yue=12;lcd_nyr(5,yue);write1602com(0 x80+6);break;case 7:nian--;if(nian==-1)nian=99;lcd_nyr(2,nian);write1602com(0 x80+3);break;}}}if(s4==0){delay(5);if(s4==0){// while(!s4);beep=0;delay(50);beep=1;num++;while(!s4);beep=0;delay(50);beep=1;// flag1=1;// flag1=~flag1;if(num==1)// if(flag1==1) //设置闹钟{flag1=1;write1602com(0x80+0x40+7);write1602com(0x0f);read_alarm();miao=amiao;fen=afen;shi=ashi;write1602com(0x80+0x40+9);write1602data('D');write1602data('i');// write1602com(0x80+0x40+7);// write1602com(0x0f);lcd_sfm(0,ashi);lcd_sfm(3,afen);lcd_sfm(6,amiao);}// else //退出闹钟设置,并把设置的时间输入进DS12C887中if(num==2){// delay(50);num=0;s1num=0;write1602com(0x80+0x40+9);write1602data(' ');write1602data(' ');// write1602com(0x0c);// ds12887_write(1,miao);// ds12887_write(3,fen);// ds12887_write(5,shi);set_alarm(shi,fen,miao);flag1=0;flag=0;}}}}}void main(){/*uchar i;init_1602();for(i=0;i<9;i++){write1602data(table0[i]);delay(5);}delay(10);write1602com(0x80+0x40);for(i=0;i<8;i++){write1602data(table1[i]);delay(5);}while(1);*/init_1602();lcd1602showsign();init_ds12887();while(1){keyscan();if(flag_di==0){// delay(100);didi();// delay(200);// flag_di=1;}// delay(10);if((flag==0)&&(flag1==0)){keyscan();// delay(10);shi=ds12887_read(0x04);fen=ds12887_read(0x02);miao=ds12887_read(0x00);nian=ds12887_read(0x09);yue =ds12887_read(0x08);ri =ds12887_read(0x07);week=ds12887_read(0x06);lcd_sfm(0,shi);lcd_sfm(3,fen);lcd_sfm(6,miao);lcd_nyr(2,nian);lcd_nyr(5,yue);lcd_nyr(8,ri);// lcd_nyr(14,week);show_week();clearnumofweek();}}}void exter0() interrupt 0{uchar c;flag_di=0;c=ds12887_read(0x0c);}。
PyQt5使用QTimer实现电子时钟
PyQt5使⽤QTimer实现电⼦时钟本⽂⽤ PyQt5 的QTimer类的两种⽅式实现电⼦时钟,供⼤家参考,具体内容如下【效果图】【知识点】QTimer类提供了定时器信号/槽和单触发定时器。
它在内部使⽤定时器事件来提供更通⽤的定时器。
QTimer很容易使⽤:创建⼀个QTimer,使⽤start()来开始并且把它的timeout()连接到适当的槽。
当这段时间过去了,它将会发射timeout()信号。
【实现】1、定时器信号/槽⽅式class MyTimer(QWidget):def __init__(self, parent = None):# ......#新建⼀个QTimer对象self.timer = QTimer()self.timer.setInterval(1000)self.timer.start()# 信号连接到槽self.timer.timeout.connect(self.onTimerOut)# 定义槽def onTimerOut(self):self.lcd.display(time.strftime("%X",time.localtime()))完整代码:import sysimport timefrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *class MyTimer(QWidget):def __init__(self, parent = None):super(MyTimer, self).__init__(parent)self.resize(200, 100)self.setWindowTitle("QTimerDemo")self.lcd = QLCDNumber()self.lcd.setDigitCount(10)self.lcd.setMode(QLCDNumber.Dec)self.lcd.setSegmentStyle(QLCDNumber.Flat)self.lcd.display(time.strftime("%X",time.localtime()))layout = QVBoxLayout()layout.addWidget(self.lcd)self.setLayout(layout)#新建⼀个QTimer对象self.timer = QTimer()self.timer.setInterval(1000)self.timer.start()# 信号连接到槽self.timer.timeout.connect(self.onTimerOut)# 定义槽def onTimerOut(self):self.lcd.display(time.strftime("%X",time.localtime()))app = QApplication(sys.argv)t = MyTimer()t.show()sys.exit(app.exec_())2、定时器事件⽅式class MyTimer(QWidget):def __init__(self, parent = None):# ......#新建⼀个QTimer对象self.timer = QBasicTimer() # QTimer()貌似不⾏,不知何故?self.timer.start(1000, self)# 覆写计时器事件处理函数timerEvent()def timerEvent(self, event):self.lcd.display(time.strftime("%X",time.localtime()))完整代码:import sysimport timefrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *class MyTimer(QWidget):def __init__(self, parent = None):super(MyTimer, self).__init__(parent)self.resize(200, 100)self.setWindowTitle("QTimerDemo")self.lcd = QLCDNumber()self.lcd.setDigitCount(10)self.lcd.setMode(QLCDNumber.Dec)self.lcd.setSegmentStyle(QLCDNumber.Flat)self.lcd.display(time.strftime("%X",time.localtime()))layout = QVBoxLayout()layout.addWidget(self.lcd)self.setLayout(layout)#新建⼀个QTimer对象self.timer = QBasicTimer() # QTimer()貌似不⾏,不知何故?self.timer.start(1000, self)# 覆写计时器事件处理函数timerEvent()def timerEvent(self, event):if event.timerId() == self.timer.timerId():self.lcd.display(time.strftime("%X",time.localtime()))else:super(WigglyWidget, self).timerEvent(event)app = QApplication(sys.argv)t = MyTimer()t.show()sys.exit(app.exec_())以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
基于单片机智能音乐闹钟源代码0财富
基于单片机智能音乐闹钟源代码/********************************************* ********************************************** ********************************************** ********************************************** ********************************************** ********************************************** ********************************************** ********************************************** ************/#include<reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code font[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0 xbf}; //uchar code tab[]={0xc40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06, 0x0e};uint code note_value[][14]={{0,63628,63835,64021,64103,64260,64400,64524,63731,63928,0,64185,64331,64463}, //低音1,2,3,4,5,6,7,#1,#2,0(#3在此程序中无需设),#4,#5,#6 相对应的定时器初值{0,64580,64684,64777,64820,64898,64968,65030,64633,64732,0,64860,64934,64994}, //中音1,2,3,4,5,6,7,#1,#2,0(#3在此程序中无需设),#4,#5,#6 相对应的定时器初值{0,65058,65110,65157,65178,65217,65252,65283,65085,65134,0,65198,65235,65268}//高音1,2,3,4,5,6,7,#1,#2,0(#3在此程序中无需设),#4,#5,#6 相对应的定时器初值};uint code song[][67]={/* 0 嘀~(按键时的嘀嗒声)*/ {0x2701,0x0},/* 1 茉莉花*/ {0x1304,0x1302,0x1502,0x1602,0x2102,0x2102,0x1602,0x1504,0x1502,0x1602,0x1507,0x1001,/*0x1304,0x1302,0x1502,0x1602,0x2102,0x2102,0x1602,0x1504,0x1502,0x1602,0x1507,0x1001,* / 0x1504,0x1504,0x1504,0x1302,0x1502,0x1604,0x1604,0x1507,0x1001, 0x1304,0x1202,0x1302,0x1504,0x1302,0x1202,0x1104,0x1102,0x1202,0x1107,0x1001,0x1302,0x1202,0x1102,0x1302,0x1204,0x1002,0x1302,0x1504,0x1602,0x2102,0x1507,0x1001, 0x1202,0x1302,0x1502,0x1202,0x1302,0x1102,0x0602,0x0507,0x1001,0x0604,0x0602,0x1102,0 x1206,0x1302,0x1102,0x1202,0x1102,0x0602,0x050d,0x0},/* 2 莫斯科郊外的晚上*/{0x0602,0x1102,0x1302,0x1102,0x1204,0x1102,0x0702,0x1304,0x1204,0x0607,0x1001,0x1102,0x1302,0x1502,0x1502,0x1604,0x1502,0x1402,0x1307,0x1001,0x1414,0x1514,0x1702,0x1602,0x1304, 0x1002,0x0704,0x0602,0x1302,0x1202,0x1404, 0x1004,0x1502,0x1402,0x1304,0x1202,0x1102,0x1304,0x1204,0x060c, 0x0},/* 3 祝你生日快乐*/ {0x0502,0x0502,0x0604,0x0504,0x1104,0x0708,0x0502,0x0502,0x0604,0x0504,0x1204,0x 1108,0x0502,0x0502,0x1504,0x1304,0x1104,0x0704,0x0604,0x1402,0x1402,0x1304,0x1104,0x1 204,0x1108,0x0},/* 4 铃儿响叮当*/ {0x1302,0x1302,0x1303,0x1001, 0x1302,0x1303,0x1001, 0x1302,0x1502,0x1102,0x1202,0x1307,0x1001,0x1402,0x1402,0x1402,0x1402,0x1402,0x1302,0x1302,0x1302,0x1302,0x1202,0x1202,0x1302,0 x1204,0x1503,0x1001, 0x1302,0x1302,0x1303,0x1001, 0x1302,0x1303,0x1001, 0x1302,0x1502,0x1102,0x1202,0x1307,0x1001,0x1402,0x1402,0x1402,0x1402,0x1402,0x1302,0x1302,0x1302,0x1502,0x1502,0x1402,0x1202,0 x1108,0x0},/* 5 喀秋莎*/ {0x0606,0x0702,0x1106,0x0602,0x1102,0x1102,0x0702,0x0602,0x0704,0x1002,0x0706,0x1102,0x1206,0x0702,0x1202,0x1202,0x1102,0x0702,0x0607,0x1001,0x1304,0x1604,0x1504,0x1602,0x1502,0x1402,0x1402,0x1302,0x1202,0x1304,0x0604,0x1002,0x1404,0x1202,0x1306,0x1102,0x0702,0x0302,0x1102,0x0702,0x0608,0x0},/* 6 欢乐颂*/ {0x1304,0x1304,0x1404,0x1504,0x1504,0x1404,0x1304,0x1204,0x1104,0x1104,0x1204,0x 1304,0x1306,0x1202,0x1207,0x1001,0x1304,0x1304,0x1404,0x1504,0x1504,0x1404,0x1304,0x1204,0x1104,0x1104,0x1204,0x1304,0 x1206,0x1102,0x1107,0x1001, 0x0},/* 7 四季歌*/ {0x1304,0x1302,0x1202,0x1104,0x1102,0x0702,0x0604,0x0604,0x0607,0x1001,0x1404,0x1402,0x1302,0x1202,0x1102,0x1202,0x1402,0x130f,0x1001,0x1404,0x1402,0x1302,0x1204,0x1202,0x1402,0x1304,0x1302,0x1102,0x0604,0x0604,0x0704,0 x1304,0x1302,0x1102,0x0702,0x1102,0x060f,0x0},/* 8 月亮代表我的心*/ {0x150a,0x1302,0x1202,0x1102,0x130f,0x160a,0x1402,0x1202,0x1102,0x070c,0x1002,0x0502, 0x1106,0x1302,0x1506,0x1102,0x0706,0x1302,0x1504,0x1002,0x1502,0x1606,0x1702,0x2106,0 x1604,0x150a,0x1302,0x1202,0x1106,0x1102,0x1104,0x1302,0x1202,0x1106,0x1102,0x1104,0x 1202,0x1302,0x1206,0x1102,0x0606,0x1201,0x1301,0x120c,0x1002,0x0502,0x1106,0x1302,0x1 506,0x1102,0x0706,0x1302,0x1504,0x1002,0x1502,0x1606,0x1702,0x2106,0x1604,0x150a,0x1302,0x1202,0x1106,0x1102,0x1104,0x1302,0x0},//0x1202,0x1106,0x1102,0x1104,0x1202,0x1302 ,0x1206,0x0602,0x0704,0x1102,011202,0x110c,0x1302,0x1502,0x1306,0x1202,0x1104,0x1504,0 x070c,0x0602,0x0702,0x0606,0x0702,0x0604,0x0504,0x130c,0x1504,0x1306,0x1202,0x1104,0x 1504,0x070c,0x0602,0x0702,0x1106,0x1102,0x1104,0x1202,0x1302,0x120c,0x1002,0x0502,0x1 106,0x1302,0x1506,0x1102,0x0706,0x1302,0x1504,0x1002,0x1502,0x1606,0x1702,0x2106,0x16 04,0x150a,0x1302,0x1202,0x1106,0x1102,0x1104,0x1302,0x1202,0x1106,0x1102,0x1104,0x120 2,0x1302,0x1206,0x0602,0x0704,0x1102,0x1202,0x1104,0x1502,0x1402,0x1304,0x1202,0x1102 ,0x1104,0x0702,0x0602,0x0518,0x050f,0x0},/* 9 献给爱丽丝*/ {0x2302,0x2212,0x2302,0x2212,0x2302,0x1702,0x2202,0x2102,0x1604,0x1002,0x1102,0x1302,0x1602,0x1704, 0x1002,0x1302,0x1512,0x1702,0x2104, 0x1002,0x1302,0x2302,0x2212,0x2302,0x2212,0x2302,0x1702,0x2202,0x2102,0x1604,0x1002,0x1102,0x1302,0x1602,0x1704, 0x1002,0x1202,0x2102,0x1702,0x1604, 0x1002,0x1702,0x2102,0x2202,0x2305,0x1001, 0x1502,0x2402,0x2302,0x2205,0x1001, 0x1402,0x2302,0x2202,0x2105,0x1001, 0x1302,0x2202,0x2102,0x1704, 0x1002,0x1302,0x2302,0x1002, 0x0},// 0x2212,0x2302, 0x1002,0x1002,0x2212,0x2302,0x2212,0x2302,0x2212,0x2302,0x1702,0x2202,0x2102,0x1604, 0x1002,0x1102,0x1302,0x1602,0x1704, 0x1002,0x1302,0x1512,0x1702,0x2104, 0x1002,0x1302,0x2302,0x2212,0x2302,0x2212,0x2302,0x1702,0x2202,0x2102,0x1604,0x1002,0x1102,0x1302,0x1602,0x1704,0x1002,0x1202,0x2102,0x1702,0x1604,0x1004,0x0},/* 10 小星星,亮晶晶*/{0x1104,0x1104,0x1504,0x1504,0x1604,0x1604,0x1507,0x1001,0x1404,0x1404,0x1304,0x1304,0x1204,0x1204,0x1107,0x1001,0x1504,0x1504,0x1404,0x1404,0x1304,0x1304,0x1207,0x1001,0x1504,0x1504,0x1404,0x1404,0x1304,0x1304,0x1207,0x1001,0x1104,0x1104,0x1504,0x1504,0x1604,0x1604,0x1507,0x1001,0x1404,0x1404,0x1304,0x1304,0x1204,0x1204,0x1107,0x1001, 0x0},/* 11 Nokia经典铃声*/ {0x2502,0x2402,0x1604,0x1704,0x2302,0x2202,0x1404,0x1504,0x2202,0x2102,0x1304,0x1504, 0x210c,0x2502,0x2402,0x1604,0x1704,0x2302,0x2202,0x1404,0x1504,0x2202,0x2102,0x1304,0x1504,0 x210c,0x0}/* 12 两只老虎*/ {0x1104,0x1204,0x1304,0x1104,0x1104,0x1204,0x1304,0x1104,0x1304,0x1404,0x1508,0x 1304,0x1404,0x1508,0x1502,0x1602,0x1502,0x1402,0x1304,0x1104,0x1502,0x1602,0x1502,0x1 402,0x1304,0x1104,0x1304,0x0504,0x1108,0x1304,0x0504,0x1108,0x0}};sbit key_select=P1^0; //功能键sbit key_inc=P1^1; // 增大建sbit key_dec=P1^2; // 减小键&强行关闹钟和歌曲键sbit key_alarm_or_song=P1^3; //调闹钟键&点歌键sbit buzzer=P0^0;bit music_break=0;uchar week=1,hour=12,minute=0,second=0, alarm_num,display[]={1,2,0,0,0,0},n=0,bit_code=0xfb,alarm_break=1,flag_display=0,flag_a larm=0; //定义数组display[6]作为显示缓冲区,n为数组下标,flag_display用于设置时间或闹钟时的闪烁显示标志,flag_alarm用于闹钟键按下的标志uchardisplay_music[6]={0x10,0x10,0,0,0x10,0x10},display_alarm[6]={0,0,0,0,0,0},alarm[][3]={{12}, {12,1},{12,3},{12,5},{1},{2},{3},{4},{5},{6}},alarm_num=0,song_num=0;uint initial_value,count_display=0,count=0; //count_display用于设置时间或闹钟时的闪烁显示记时变量void delay_ms(uint t) //延时1ms{uint i,j;for(i=t;i>0;i--)for(j=110;j>0;j--);}void di() //按键发声函数{buzzer=0;delay_ms(100);buzzer=1;}void music(uchar song_number){uchar i,section,basic_note,changed_note,pitch,duration,note_num=0; //分别定义i,音区,基本音级,变化音级,音高,时值,所选歌曲的音符的编号uint note; //定义音符变量ET1=1;music_break=1;note=song[song_number][note_num];while(note!=0&&music_break){section=note/0x1000;basic_note=note/0x100%0x10;changed_note=note/0x10%0x10;duration=note%0x10;if(changed_note==1)pitch=basic_note+7;pitch=basic_note;initial_value=note_value[section][pitch];if(initial_value!=0){TH1=initial_value/256;TL1=initial_value%256;TR1=1;}elseTR1=0;if(key_alarm_or_song==0){delay_ms(10);if(key_alarm_or_song==0)break;}for(i=duration;i>0;i--) //音符延时函数delay_ms(125);note_num++;note=song[song_number][note_num];if(key_dec==0) //强行关闭闹钟{delay_ms(10);if(key_dec==0){while(!key_dec);break;}}}music_break=0;TR1=0;}void alarm_(){for(alarm_num=1;alarm_num<10;alarm_num++) //添加闹钟时需要修改的地方!!!!!!!!!!if(hour==alarm[alarm_num][0]&&minute==alarm[alarm_num][1]&&second==alarm[alarm_ num][2]){display_alarm[0]=alarm[alarm_num][0]/10;display_alarm[1]=alarm[alarm_num][0]%10;display_alarm[2]=alarm[alarm_num][1]/10;display_alarm[3]=alarm[alarm_num][1]%10;display_alarm[4]=0x10;display_alarm[5]=alarm_num;music(alarm_num);}}/*void alarm_ () //闹钟发声函数{if(hour==alarm[0]&&minute==alarm[1]&&second==alarm[2]){uint k;for(k=10;k>0;k--){buzzer=0;delay_ms(100);buzzer=1;}}}*/void scan_key() //按键扫描函数{bit flag_break=0; //flag_break作为跳出按键扫描循环程序的标志位if(key_select==0){delay_ms(10);if(key_select==0) // 确认功能键被按下{while(!key_select); //释放确认di();while(1) //时按键处理{flag_display=1;if(flag_break==1){alarm_num=0;break;}if(key_alarm_or_song==0){delay_ms(10);if(key_alarm_or_song==0){flag_alarm=1;if(flag_break==1){alarm_num=0;break;}alarm_num++;if(alarm_num==10) //添加闹钟时需要修改的地方!!!!!!!!!!alarm_num=1;display_alarm[0]=alarm[alarm_num][0]/10;display_alarm[1]=alarm[alarm_num][0]%10;display_alarm[2]=alarm[alarm_num][1]/10;display_alarm[3]=alarm[alarm_num][1]%10;display_alarm[4]=0x10;display_alarm[5]=alarm_num%10;while(!key_alarm_or_song);di();}}if(key_select==0){delay_ms(10);if(key_select==0) //确认功能键改时被按下{while(!key_select); //释放确认di();while(1) //分按键处理{flag_display=2;if(flag_break==1)break;if(key_select==0){delay_ms(10);if(key_select==0) //确认功能键改分被按下{while(!key_select); //释放确认di();while(1) //秒按键处理{flag_display=3;if(key_select==0){delay_ms(10);if(key_select==0)//确认功能键改秒被按下{while(!key_select);//此处等待按键释放非常重要!!!di();flag_alarm=0;flag_break=1;break;}}if(key_inc==0&&flag_alarm==0){delay_ms(10);if(key_inc==0){if(flag_alarm==0){second++;if(second==60)second=0;display[4]=second/10;display[5]=second%10;while(!key_inc);}else{alarm[alarm_num][2]++;if(alarm[alarm_num][2]==60)alarm[alarm_num][2]=0;display_alarm[4]=0x10;display_alarm[5]=alarm_num;while(!key_inc);}di();}}if(key_dec==0){delay_ms(10);if(key_dec==0){if(flag_alarm==0){second--;if(second==255)second=59;display[4]=second/10;display[5]=second%10;while(!key_dec);}else{alarm[alarm_num][2]--;if(alarm[alarm_num][2]==255)alarm[alarm_num][2]=59;display_alarm[4]=0x10;display_alarm[5]=alarm_num;while(!key_dec);}di();}}}}}if(key_inc==0){delay_ms(10);if(key_inc==0){if(flag_alarm==0){minute++;if(minute==60)minute=0;display[2]=minute/10;display[3]=minute%10;while(!key_inc);}else{alarm[alarm_num][1]++;if(alarm[alarm_num][1]==60)alarm[alarm_num][1]=0; display_alarm[2]=alarm[alarm_num][1]/10;display_alarm[3]=alarm[alarm_num][1]%10;while(!key_inc);}di();}}if(key_dec==0){delay_ms(10);if(key_dec==0){if(flag_alarm==0){minute--;if(minute==255)minute=59;display[2]=minute/10;display[3]=minute%10;while(!key_dec);}else{alarm[alarm_num][1]--;if(alarm[alarm_num][1]==255)alarm[alarm_num][1]=59; display_alarm[2]=alarm[alarm_num][1]/10;display_alarm[3]=alarm[alarm_num][1]%10;while(!key_dec);}di();}}}}}if(key_inc==0){delay_ms(10);if(key_inc==0){if(flag_alarm==0){hour++;if(hour==24)hour=0;display[0]=hour/10;display[1]=hour%10;while(!key_inc);}else{alarm[alarm_num][0]++;if(alarm[alarm_num][0]==24)alarm[alarm_num][0]=0;display_alarm[0]=alarm[alarm_num][0]/10;display_alarm[1]=alarm[alarm_num][0]%10;while(!key_inc);}di();}}if(key_dec==0){delay_ms(10);if(key_dec==0){if(flag_alarm==0){hour--;if(hour==255)hour=23;display[0]=hour/10;display[1]=hour%10;while(!key_dec);}else{alarm[alarm_num][0]--;if(alarm[alarm_num][0]==255)alarm[alarm_num][0]=23;display_alarm[0]=alarm[alarm_num][0]/10;display_alarm[1]=alarm[alarm_num][0]%10;while(!key_dec);}di();}}}}}flag_display=0;if(key_alarm_or_song==0){delay_ms(10);if(key_alarm_or_song==0){while(!key_alarm_or_song);song_num++;if(song_num==12)song_num=1; //添加歌曲时需要修改的地方!!!!!!!!!!display_music[2]=song_num/10;display_music[3]=song_num%10;music(song_num);}}}void main(){TMOD=0x11;EA=1;ET0=1;ET1=1;TH0=0xf6; //定时50ms 初值TL0=0x3c;TR0=1;PT1=1;PT0=0;while(1){ buzzer=1;alarm_num=0;scan_key();alarm_();}}void timer_0() interrupt 1{TH0=0xf6;TL0=0x3c;count++;if(count==400){count=0;second++;display[4]=second/10;display[5]=second%10;if(second==60){second=0;display[4]=second/10;display[5]=second%10;minute++;display[2]=minute/10;display[3]=minute%10;if(minute==60){minute=0;display[2]=minute/10;display[3]=minute%10;hour++;if(hour==24)hour=0;display[0]=hour/10;display[1]=hour%10;}}}if(flag_alarm==0){if(music_break==1&&alarm_num==0)P3=font[display_music[n]];else{if(music_break==1&&alarm_num!=0){if(n==1)P3=font[display_alarm[n]]&0x7f;elseP3=font[display_alarm[n]];}else{if(n==1||n==3)P3=font[display[n]]&0x7f;elseP3=font[display[n]];}}}elseif(n==1)P3=font[display_alarm[n]]&0x7f;elseP3=font[display_alarm[n]];n++;if(n==6)n=0;switch(flag_display) //调时、调闹钟闪烁部分{case 0: {P2=bit_code;bit_code=_crol_(bit_code,1);if(bit_code==0xfe)bit_code=0xfb;} break;case 1: {count_display++;if(count_display<=200){P2=bit_code;bit_code=_crol_(bit_code,1);if(bit_code==0xfe)bit_code=0xfb;}if(count_display>200&&count_display<=400){if(count_display==400)count_display=0;if(bit_code==0xfb||bit_code==0xf7)P2=0xff;elseP2=bit_code;bit_code=_crol_(bit_code,1);if(bit_code==0xfe)bit_code=0xfb;}} break;case 2: {count_display++;if(count_display<=200){P2=bit_code;bit_code=_crol_(bit_code,1);if(bit_code==0xfe)bit_code=0xfb;}if(count_display>200&&count_display<=400){if(count_display==400)count_display=0;if(bit_code==0xef||bit_code==0xdf)P2=0xff;elseP2=bit_code;bit_code=_crol_(bit_code,1);if(bit_code==0xfe)bit_code=0xfb;}} break;case 3: {if(flag_alarm==0){count_display++;if(count_display<=200){P2=bit_code;bit_code=_crol_(bit_code,1);if(bit_code==0xfe)bit_code=0xfb;}if(count_display>200&&count_display<=400){if(count_display==400)count_display=0;if(bit_code==0xbf||bit_code==0x7f)P2=0xff;elseP2=bit_code;bit_code=_crol_(bit_code,1);if(bit_code==0xfe)bit_code=0xfb;}}else{P2=bit_code;bit_code=_crol_(bit_code,1);if(bit_code==0xfe)bit_code=0xfb;}} break;default : break;}}void timer_1() interrupt 3{TH1=initial_value/256;TL1=initial_value%256;buzzer=~buzzer;}/****************************************************************************** ******************************************************************************* ******************************************************************************* ******************************************************************************* ******************************************************************************* ******************************************************************************* ******************************************************************************* ***************/。
QT定时器的使用方法
QT定时器的使⽤⽅法在界⾯程序中很容易使⽤到,定时刷新或者更新什么东西,此时应该使⽤定时器的功能,定时器是在指定时间触发定时器函数,来达到定时的效果接下来介绍两种定时器的使⽤,废话不说直接上代码代码结构:dialog.h#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include <QTimerEvent>#include <QDebug>#include <QTimer>namespace Ui {class Dialog;}class Dialog : public QDialog{Q_OBJECTpublic:explicit Dialog(QWidget *parent = nullptr);~Dialog();enum timerIndex //枚举从 0 开始{timer1,timer2,timer3};private:Ui::Dialog *ui;QTimer *update_time;int id1,id2,id3;void timerEvent(QTimerEvent *event);private slots:void time_update();};#endif// DIALOG_Hdialog.cpp#include "dialog.h"#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog){ui->setupUi(this);id1 = startTimer(1000);id2 = startTimer(2000);id3 = startTimer(3000);update_time = new QTimer();connect(update_time,SIGNAL(timeout()),this,SLOT(time_update()));update_time->start(1000); //1秒钟后启动}void Dialog::timerEvent(QTimerEvent *event){qDebug() << event->timerId() << endl;switch (event->timerId()-1){case timer1 :qDebug() << "timer1" << endl;break;case timer2 :qDebug() << "timer2" << endl;break;case timer3 :qDebug() << "timer3" << endl;break;default:qDebug() << "no !!"<<endl;break;}}void Dialog::time_update(){qDebug() <<"update"<< endl;}Dialog::~Dialog(){delete ui;}main.cpp#include "dialog.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);Dialog w;w.show();return a.exec();}两种定时器使⽤⽅法已经介绍,可以根据⾃⼰的实际情况进⾏选择使⽤,个⼈感觉推荐使⽤获取定时器id 的那种⽅法,⽐较⽅便⼯程代码在此链接需要⾃⾏下载链接:https:///s/1iM3IgRzfEoOD21ek1L_GVQ提取码:hcqy。
PyQt5中QTimer定时器的实例代码
PyQt5中QTimer定时器的实例代码如果要在应⽤程序中周期性地进⾏某项操作,⽐如周期性地检测主机的CPU值,则需要⽤到QTimer定时器,QTimer类提供了重复的和单次的定时器。
要使⽤定时器,需要先创建⼀个QTimer实例,将其timeout信号连接到相应的槽,并调⽤start()。
然后定时器会以恒定的间隔发出timeout信号,当窗⼝控件收到timeout信号后,它就会停⽌这个定时器。
⼀、QTimer类中的常⽤⽅法⽅法描述start(milliseconds)启动或重新启动定时器,时间间隔为毫秒。
如果定时器已经运⾏,它将被停⽌并重新启动。
如果singleShot信号为真,定时器将仅被激活⼀次Stop()停⽌定时器⼆、QTimer类中的常⽤信号信号描述singleShot在给定的时间间隔后调⽤⼀个槽函数时发射此信号timeout当定时器超时时发射此信号三、QTimer的使⽤⽰例1:import sysfrom PyQt5 import QtCorefrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import *class Demo(QWidget):count = 0def __init__(self):super().__init__()self.setGeometry(100, 50, 500, 400)self.setWindowTitle('QTimer')self.list = QListWidget()bel = QLabel('显⽰当前时间')self.start = QPushButton('开始')self.end = QPushButton('结束')layout = QGridLayout()#初始化定时器self.timer = QTimer(self)self.timer.timeout.connect(self.showTime)self.start.clicked.connect(self.startTimer)self.end.clicked.connect(self.endTimer)layout.addWidget(bel,0,0,1,2)layout.addWidget(self.start,1,0)layout.addWidget(self.end,1,1)self.setLayout(layout)def showTime(self):#获取系统现在的时间time = QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd')bel.setText(time)def startTimer(self):#设置时间间隔并启动定时器self.timer.start(1000)self.start.setEnabled(False)self.end.setEnabled(True)def endTimer(self):#关闭定时器self.timer.stop()self.start.setEnabled(True)self.end.setEnabled(False)if __name__ == "__main__":app = QApplication(sys.argv)form = Demo()form.show()sys.exit(app.exec_())运⾏效果如下:⽰例2:import sysfrom PyQt5 import QtCorefrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import *if __name__ == "__main__":app = QApplication(sys.argv)label = QLabel('<font color=blue size=20><b>PyQt5,窗⼝5秒后消失</b></font>')#⽆边框窗⼝label.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)label.show()#设置5秒后⾃动退出QTimer.singleShot(5000,app.quit)sys.exit(app.exec_())运⾏效果如下:PyQt5 QTimer计数到特定的秒数我正在使⽤python创建程序,并且正在使⽤pyqt。
qt 实现定时任务的方法
在Qt中实现定时任务有多种方法,以下是其中几种常见的方法:
1. QTimer类:QTimer是Qt提供的一个定时器类,可以用于执行定时任务。可以通过设 置定时器的时间间隔和连接相应的槽函数来实现定时任务的执行。例如:
```cpp QTimer* timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(doTask())); timer->start(1000); // 每隔1秒执行一次任务 ```
```cpp class MyThread : public QThread {
void run() override { while (true) { // 执行任务逻辑 sleep(1); // 每隔1秒执行一次任务 }
} };
qt 实现定时任务的方法
// 在主线程中创建并启动子线程 MyThread* thread = new MyThread(); thread->start(); ```
} }); ```
qt 实现定时任务的方法
以上是几种常见的在Qt中实现定时任务的方法。具体选择哪种方法取决于你的需求和场 景,可以根据实际情况选择最适合的方法。
qt 实现定时任务的方法
3. QtConcurrent库:QtConcurrent是Qt提供的一个并发编程库,可以用于执行并行任 务。其中的`QTimer`类可以用于执行定时任务。例如:
```cpp QFuture<void> future = QtConcurrent::run([=]() {
while (true) { // 执行任务逻辑 QThread::msleep(1000); // 每隔1秒执行一次任务
闹钟提醒的程序设计及代码示例
闹钟提醒的程序设计及代码示例闹钟提醒是人们日常生活中常用的功能之一,它可以帮助我们准时起床、提醒重要事件等。
在计算机编程中,我们可以通过程序设计实现一个闹钟提醒系统。
下面,将介绍闹钟提醒程序的设计原理、流程以及代码示例。
一、程序设计原理闹钟提醒程序的设计原理主要包括以下几个步骤:1. 用户输入设定的闹钟时间;2. 程序获取当前时间;3. 比较当前时间和设定的闹钟时间;4. 如果当前时间等于设定的闹钟时间,则触发提醒;5. 如果当前时间不等于设定的闹钟时间,则等待一段时间后重新执行第2步。
二、程序设计流程根据上述原理,我们可以设计出如下的闹钟提醒程序流程:1. 用户输入设定的闹钟时间,并保存到变量alarm_time中;2. 进入循环,不断执行以下步骤:1) 获取当前时间,并保存到变量current_time中;2) 比较current_time和alarm_time;3) 如果current_time等于alarm_time,则触发提醒,提示用户;4) 如果current_time不等于alarm_time,则等待一段时间后再次执行循环。
三、代码示例下面是一个使用Python语言编写的闹钟提醒程序代码示例:```pythonimport datetimeimport timedef alarm_clock(alarm_time):while True:current_time = datetime.datetime.now().strftime("%H:%M")if current_time == alarm_time:print("时间到了!该起床啦!")breakelse:print("当前时间:", current_time, "闹钟时间:", alarm_time)time.sleep(60) # 每隔60秒检查一次alarm_time = input("请输入设定的闹钟时间(如08:00):")alarm_clock(alarm_time)```在这个示例代码中,我们使用了Python内置的`datetime`和`time`模块来获取当前时间和控制程序等待的时间。
qt中定时器Timer的使用[指南]
qt中定时器Timer的使用qt中定时器Timer的使用有两种方法实现定时器。
第一种。
自己建立关联。
1.新建Gui工程,工程名可以设置为timer。
并在主界面上添加一个标签label,并设置其显示内容为“0000-00-00 00:00:00 星期日”。
2.在mainwindow.h中添加槽函数声明。
private slots:void timerUpDate();3.在mainwindow.cpp中添加代码。
添加#include <QtCore>的头文件包含,这样就包含了QtCore下的所有文件。
构造函数里添加代码:QTimer *timer = new QTimer(this);//新建定时器connect(timer,SIGNAL(timeout()),this,SLOT(timerUpDate()));//关联定时器计满信号和相应的槽函数timer->start(1000);//定时器开始计时,其中1000表示1000ms即1秒4.然后实现更新函数。
void MainWindow::timerUpDate(){QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); //设置系统时间显示格式ui->label->setText(str);//在标签上显示时间}5.运行程序,效果如下。
第二种。
使用事件。
(有点像单片机中的定时器啊)1.新建工程。
在窗口上添加两个标签。
2.在main.cpp中添加代码,实现中文显示。
#include <QTextCodec>QTextCodec::setCodecForTr(QTextCodec::codecForLocale());3.在mainwindow.h中添加代码。
基于Qt5的闹钟实现代码
本程序基于QWidget实现,可以动态显示钟表,并且可以添加铃声头文件widget.h代码如下:#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include<QTime>#include<QTimer>#include<QLabel>#include<QPushButton>#include<QVBoxLayout>#include<QHBoxLayout>#include<QTimeEdit>#include<QLCDNumber>#include<QVariant>#include<QAction>#include<QMenu>#include<QCloseEvent>#include<QApplication>#include<QMessageBox>#include<QSystemTrayIcon>namespace Ui {class Widget;}class Widget : public QWidget{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();signals:void isCurrentTime();private:Ui::Widget *ui;void init();void layout();void con();QLCDNumber *lcdNumber;QLabel *label;QTimeEdit *timeEdit;QPushButton *button;QTimer *timer;QTimer *uptimer;QTimer *dtimer;QTimer *itimer;QRect rect;int x;int y;double du;QSystemTrayIcon *trayIcon;QAction *restoreAct;QAction *quitAct;QMenu *trayIconMenu;private slots:void clockChange();void dialogUp();void mini();void down();void comToCur();protected:void closeEvent(QCloseEvent *e); };#endif // WIDGET_H源文件widget.cpp代码如下:#include "widget.h"#include "ui_widget.h"#include<QtDebug>#include<QApplication>#include<QDesktopWidget>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget){ui->setupUi(this);init();layout();con();void Widget::init(){setWindowTitle("我的闹钟");setFixedSize(300,80);timer=new QTimer;uptimer=new QTimer;dtimer=new QTimer;itimer=new QTimer;lcdNumber=new QLCDNumber;lcdNumber->setFixedSize(150,50);lcdNumber->setDigitCount(12);lcdNumber->setSegmentStyle(QLCDNumber::Flat);lcdNumber->display(QTime::currentTime().toString("hh:mm:ss:zzz"));label=new QLabel("闹钟");timeEdit=new QTimeEdit;timeEdit->setDisplayFormat("hh:mm:ss");button=new QPushButton("确定");trayIcon=new QSystemTrayIcon;trayIcon->setIcon(QIcon(":/new/prefix1/clock.ico"));trayIcon->show();setWindowIcon(QIcon(":/new/prefix1/clock.ico"));restoreAct=new QAction("显示",this);quitAct=new QAction("退出",this);trayIconMenu=new QMenu;trayIconMenu->addAction(restoreAct);trayIconMenu->addAction(quitAct);trayIcon->setContextMenu(trayIconMenu);setWindowFlags(Qt::WindowStaysOnTopHint);}void Widget::layout(){QHBoxLayout *rightTopLay=new QHBoxLayout;rightTopLay->addWidget(label);rightTopLay->addWidget(timeEdit);QHBoxLayout *rightButtonLay=new QHBoxLayout;rightButtonLay->addStretch();rightButtonLay->addWidget(button);QVBoxLayout *rightLay=new QVBoxLayout;rightLay->addLayout(rightTopLay);rightLay->addLayout(rightButtonLay);rightLay->addStretch();QHBoxLayout *m=new QHBoxLayout;m->addWidget(lcdNumber);m->addLayout(rightLay);setLayout(m);QDesktopWidget *p=new QDesktopWidget;p=QApplication::desktop();//rect=p->rect();x=rect.right()-width()-2;//y=rect.bottom()+30;//setGeometry(x,y,width(),height());}void Widget::con(){connect(timer,SIGNAL(timeout()),this,SLOT(clockChange()));timer->start(1);connect(uptimer,SIGNAL(timeout()),this,SLOT(dialogUp()));uptimer->start(20);connect(dtimer,SIGNAL(timeout()),this,SLOT(down()));dtimer->start(20);connect(button,SIGNAL(clicked(bool)),dtimer,SIGNAL(timeout())); connect(button,SIGNAL(clicked(bool)),this,SLOT(mini()));connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur()));itimer->start(20);connect(restoreAct,SIGNAL(triggered(bool)),uptimer,SIGNAL(timeout())) ;connect(this,SIGNAL(isCurrentTime()),uptimer,SIGNAL(timeout())); connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur()));itimer->start(20);connect(quitAct,SIGNAL(triggered(bool)),this,SLOT(close()));void Widget::clockChange(){lcdNumber->display(QTime::currentTime().toString("hh:mm:ss:zzz")); }void Widget::dialogUp(){show();uptimer->start(20);if(y>=rect.bottom()-height()-50){y-=2;}else {uptimer->stop();}setGeometry(x,y,width(),height());}void Widget::down(){dtimer->start(20);if(y<rect.bottom()){y+=height()+20;}else{dtimer->stop();}setGeometry(x,y,width(),height());}void Widget::mini(){hide();}void Widget::closeEvent(QCloseEvent *e){QMessageBox::information(0,"退出","您确定要退出吗?");void Widget::comToCur(){if(QVariant(QTime::currentTime().toString())==QVariant(timeEdit->time ()).toString()){QApplication::beep();emit isCurrentTime();}}Widget::~Widget(){delete ui;}程序运行截图:添加铃声:。
Qt实现指针式时钟Qt实现动态时钟
Qt实现指针式时钟Qt实现动态时钟本⽂实例为⼤家分享了Qt实现指针式时钟、动态时钟的具体代码,供⼤家参考,具体内容如下先上图:点击运⾏后⾸先是⼀个指针式时钟窗⼝,点击Digital Clock->可以跳转到数字时钟窗⼝,再点击Move Clock->可以还原为指针式时钟窗⼝关于整个程序的讲解都在代码注释中给出,很详细~概要:我设计两个窗⼝,⼀个主窗⼝⼀个⼦窗⼝,利⽤按钮+信号与槽机制,实现两个窗⼝的互相切换,其中主窗⼝⽤来显⽰指针时钟,完成三个基本要求:1、正确显⽰系统时钟;2、能准确定位时钟刻度和时分秒针的位置;3、能随窗⼝⼤⼩的变化⽽变化;关于主窗⼝的实现,⾸先利⽤Qt⾃带的时间函数QTime::currentTime()获取系统时间,然后利⽤paintEvent(QPaintEvent *)函数根据获取到的系统时间进⾏时针,分针,秒针的绘画,并且画出对应的⼩时刻度线,分钟刻度线,秒刻度线,实现基本时钟的样式,最后加⼊scale()函数进⾏相应的⽐例缩放,实现时钟能随窗⼝的⼤⼩变化⽽变化;关于⼦窗⼝,是我⾃⼰多加⼊的模块,是⼀个电⼦时钟,⽤⼀个lcd液晶显⽰器以”时:分:秒”的格式显⽰当下时间,同样是利⽤⾃带的时间函数QTime::currentTime()获取当前时间并通过lcd显⽰。
最后将两个窗⼝通过两个按钮连接起来,实现互相切换功能。
mainwindow.h(主窗⼝)#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include<QLCDNumber>#include<QLabel>#include<sub.h>#include<QPushButton>QT_BEGIN_NAMESPACEQT_END_NAMESPACEclass MainWindow : public QMainWindow{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);//构造函数~MainWindow();//析构函数void paintEvent(QPaintEvent *);//画时钟函数public:void dealsub();//转换为⼦窗⼝void changeback();//转换为主窗⼝private:sub w;//⼦窗⼝QPushButton b;//按钮};#endif // MAINWINDOW_Hmainwindow.cpp(主窗⼝)#include "mainwindow.h"#include "ui_mainwindow.h"#include<QPainter>#include<QPen>#include<QTime>#include<QTimer>#include<QLabel>#include<QPushButton>#include<QLCDNumber>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent){setWindowIcon(QIcon(":/new/prefix1/v2-d858191577356128b31c88e186eea0db_r.jpg"));//设置图标QTimer *timer = new QTimer(this);//产⽣⼀个定时器connect(timer, SIGNAL(timeout()), this, SLOT(update()));//关联定时器的信号与槽timer->start(1000);//开始定时器,每⼀秒钟更新⼀次resize(600,600);//窗⼝⼤⼩b.setParent(this);//指定按钮⽗对象b.setGeometry(0,0,160,40);//设置按钮位置b.setText("Digital clock->");//设置按钮内容b.setStyleSheet("QPushButton{background-color: rgba(205,214,216,0);color:rgb(0,0,0);}");//设置按钮样式,rgba前三个参数同rgb,最后⼀个参数表⽰不透明度,0~1之间 connect(&b,&QPushButton::released,this,&MainWindow::dealsub);//⿏标松开时触发,跳转到处理⼦窗⼝函数void(sub::*funsignal)()=&sub::mysignal;connect(&w,funsignal,this,&MainWindow::changeback);//关联信号与槽,实现从⼦窗⼝返回到主窗⼝}MainWindow::~MainWindow(){}void MainWindow::paintEvent(QPaintEvent *){static const QPoint hour[3] = {QPoint(14, 15),QPoint(-14, 15),QPoint(0, -110)};static const QPoint minute[3] = {QPoint(11, 13),QPoint(-11, 13),QPoint(0, -170)};static const QPoint second[3] = {QPoint(7, 8),QPoint(-7, 8),QPoint(0, -210)};int size=qMin(width(),height());QTime time=QTime::currentTime();//获取系统当前时间QPainter p(this);//创建画家对象p.setRenderHint(QPainter::Antialiasing);//防⽌图形⾛样p.translate(width()/2,height()/2);//平移坐标系置中⼼p.scale(size/600.0,size/600.0);//缩放QBrush brush;//定义画刷brush.setColor(QColor(245,182,96));//设置画刷颜⾊brush.setStyle(Qt::SolidPattern);//设置样式QPen pen;//定义画笔pen.setWidth(18);//设置画笔宽度pen.setColor(QColor(205,214,216));//rgb设置颜⾊pen.setStyle(Qt::SolidLine);//设置风格p.setPen(pen);//将画笔交给画家p.drawEllipse(QPoint(0,0),280,280);//画圆pen.setColor(Qt::white);pen.setWidth(160);//设置画笔宽度p.setPen(pen);//将画笔交给画家p.drawEllipse(QPoint(0,0),160,160);//画圆//画时针p.setBrush(brush);//将画刷交给画家p.setPen(Qt::NoPen);p.save();//保存当下状态p.rotate(30.0*(time.hour()+time.minute()/60.0));//图形旋转,以原点为旋转中⼼,顺时针⽔平旋转对应时针的⾓度p.drawConvexPolygon(hour,3);//画时针这个凸多边形,第⼀个参数为所有的点,第⼆个参数为点的个数p.restore();//恢复上⼀次保存的结果,和save()成对出现//绘制⼩时线pen.setStyle(Qt::SolidLine);pen.setWidth(5);pen.setColor(Qt::black);p.setPen(pen);for(int i=0;i<12;i++){p.drawLine(0,268,0,276);//画⼩时线p.drawText(-5,-235,QString::number(i));//表明⼩时数p.rotate(30);//每画⼀次旋转30度}//画分针p.setPen(Qt::NoPen);p.setBrush(QColor(144,199,247));p.save();//保存当下状态p.rotate(6.0*(time.minute()+time.second()/60.0));//顺时针旋转⾄分针的位置p.drawConvexPolygon(minute,3);//画分针这个凸多边形,第⼀个参数为所有的点,第⼆个参数为点的个数 p.restore();//恢复上⼀次保存的结果,和save()成对出现//绘制分钟线pen.setStyle(Qt::SolidLine);pen.setColor(QColor(0,0,0));pen.setWidth(1);p.setPen(pen);for(int i=0;i<60;i++){if((i%5)!=0)p.drawLine(0,265,0,276);//5的倍数时不画,因为有⼩时线p.rotate(6);//每画⼀次旋转6度}//画秒线p.setPen(Qt::NoPen);p.setBrush(QColor(119,217,175));p.save();p.rotate(6*time.second());//顺时针旋转⾄秒针的位置p.drawConvexPolygon(second, 3);//画秒针这个凸多边形,第⼀个参数为所有的点,第⼆个参数为点的个数 p.restore();//画圆⼼p.setBrush(Qt::black);p.setPen(Qt::white);p.save();p.drawEllipse(QPoint(0,0),3,3);//画圆⼼p.restore();//表明上午还是下午p.setPen(Qt::black);if(time.hour()>=12)p.drawText(-6,-50,"PM");//画⽂本区elsep.drawText(-6,-50,"AM");//画⽂本区p.drawText(-60,-130,"Made By ZSR");//画⽂本区}void MainWindow::dealsub(){w.show();//显⽰⼦窗⼝this->hide();//主窗⼝隐藏}void MainWindow::changeback(){w.hide();//⼦窗⼝隐藏this->show();//显⽰主窗⼝L}sub.h(⼦窗⼝)#ifndef SUB_H#define SUB_H#include <QMainWindow>#include<QPushButton>#include<QLCDNumber>class sub : public QMainWindow{Q_OBJECTpublic:explicit sub(QWidget *parent = nullptr);void sentsignal();//发送信号void paintEvent(QPaintEvent *event);//画电⼦时钟signals://信号void mysignal();public slots://槽void showtime();//显⽰时间函数private:QPushButton b1;//按钮QLCDNumber *lcd;//lcd};#endif // SUB_Hsub.cpp(⼦窗⼝)#include "sub.h"#include<QTime>#include<QTimer>#include<QLCDNumber>#include<QPainter>sub::sub(QWidget *parent) : QMainWindow(parent){setWindowIcon(QIcon(":/new/prefix1/f56513788384645db768d0ec542dec33_r.jpg"));//设置图标this->setWindowTitle("Digital clock");//设置窗⼝标题this->resize(900,500);//设置窗⼝⼤⼩b1.setParent(this);//指按钮定⽗对象b1.setText("Move clock->");//设置按钮内容b1.setGeometry(0,0,140,40);//设置按钮位置b1.setStyleSheet("QPushButton{background-color: rgba(205,214,216,0);color:rgb(0,0,0);}");//设置按钮风格connect(&b1,&QPushButton::clicked,this,&sub::sentsignal);//连接信号与槽,当点击按钮的时候跳转到发送信号函数,主窗⼝接收,再执⾏changeback()函数,即实现了跳回主窗⼝ QTimer *timer1=new QTimer(this);////产⽣⼀个定时器timer1->start(1000);//开始定时器,每⼀秒钟更新显⽰时间connect(timer1,SIGNAL(timeout()),this,SLOT(showtime()));//关联定时器的信号与槽,1s到即更新显⽰时间lcd=new QLCDNumber();//创建⼀个lcd液晶显⽰器lcd->setSegmentStyle(QLCDNumber::Filled);//设置显⽰器风格lcd->setParent(this);//指定显⽰器⽗对象lcd->move(0,50);//移动显⽰器位置lcd->setDigitCount(8);//设置所显⽰的位数为8位lcd->resize(200,50);//设置显⽰器⼤⼩showtime();//显⽰时间}void sub::sentsignal()//发送信号{emit mysignal();}void sub::showtime(){QTime time1=QTime::currentTime();//获取当前时间QString text=time1.toString("hh:mm:ss");//定义时间显⽰格式if((time1.second()%2)==0)text[5]=' ';//每2s冒号消失⼀次lcd->display(text);//lcd显⽰时间}void sub::paintEvent(QPaintEvent *event)//{QPainter p(this);//创建⼀个画家对象p.drawPixmap(rect(),QPixmap(":/new/prefix1/f8fa6c0b00b51e33e8949627d52942ea.jpg"));//设置背景图}main.cpp(主函数)#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);MainWindow w;//创建⼀个主窗⼝w.setWindowTitle("Move clock");//设置主窗⼝标题w.show();//显⽰主窗⼝return a.exec();}THE END以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
qt5开发及实例代码
qt5开发及实例代码Qt5 是一个跨平台的应用程序开发框架,它提供了一个强大的工具集和 API,用于创建各种类型的应用程序,包括桌面应用程序、移动应用程序和嵌入式设备应用程序等。
下面是一个简单的 Qt5 开发实例代码,它创建了一个窗口,并在窗口中显示了一个标签和一个按钮。
```cpp#include <QApplication>#include <QLabel>#include <QPushButton>#include <QHBoxLayout>int main(int argc, char *argv[]){QApplication app(argc, argv);// 创建一个窗口QWidget window;window.setWindowTitle("Hello, Qt5");// 创建一个标签QLabel label("Hello, World!");label.setAlignment(Qt::AlignCenter);// 创建一个按钮QPushButton button("Click Me!");button.setAlignment(Qt::AlignCenter);// 创建一个水平布局,并将标签和按钮添加到布局中QHBoxLayout layout;layout.addWidget(&label);layout.addWidget(&button);// 将布局设置为窗口的主布局window.setLayout(&layout);// 显示窗口window.show();return app.exec();}```这个实例代码创建了一个简单的 Qt5 窗口应用程序,窗口中包含一个标签和一个按钮。
标签显示了文本"Hello, World!",按钮显示了文本"Click Me!"。
毕业设计基于单片机的智能数字闹钟程序代码
辉虎的毕业设计--基于单片机的智能数字闹钟程序代码程序代码main.c#include <reg51.h>#include <intrins.h>#include <yf.h>unsigned char clock_data[4];sbit speaker = P2^4;// 蜂鸣器sbit bell = P2^2; //铃响bit bell_on=0;bit key_ok=0;unsigned char bell_time=0x20;uchar week_clock_set;#define FIRST_ADDR 0//定义全局变量unsigned char yy,mo,dd,xq,hh,mm,ss;// 定义时间映射全局变量(专用寄存器)uchar j;/************************************************************************** **///函数声明extern unsigned char read_clock(unsigned char ord);extern void lcdwc(unsigned char cmdcode);extern void hzkdis(unsigned char code *s);extern void hzkdis2(unsigned char *s);extern char Read_One_Byte(unsigned char addr);extern void Write_One_Byte(unsigned char addr,unsigned char thedata);void lcm_w_ss(void);extern void lcdreset(void);extern void Init_DS18B20(void);extern char gotkey();extern void Init_1302(void);extern unsigned int ReadTemperature(void);extern void write_clock(unsigned char ord, unsigned char dd);extern void disp_hz(uchar x,uchar *c_str,uchar w);extern void delay(unsigned int t);void new_time(void);void read_clockss(x);//void lcm_w_temp(void) ;void read_clockS(void){ss = read_clock(0x81);//读取秒数据mm = read_clock(0x83);//读取分钟数据hh = read_clock(0x85);//小时dd = read_clock(0x87);//日mo = read_clock(0x89);//月xq = read_clock(0x8b);//星期yy = read_clock(0x8d);//年}/*void Set_time(unsigned char sel){ //根据选择调整的相应项目加1并写入DS1302 signed char address,item;signed char max,mini;if(sel==7) {address=0x80; max=0;mini=0;} //秒if(sel==6) {address=0x82; max=59;mini=0;} //分钟if(sel==5) {address=0x84; max=23;mini=0;} //小时if(sel==3) {address=0x86; max=31;mini=1;} //日if(sel==2) {address=0x88; max=12;mini=1;} //月if(sel==1) {address=0x8c; max=99; mini=0;} //年if(sel==4) {address=0x8a; max=7;mini=1;} //星期//读取1302某地址上的数值转换成10进制赋给itemitem=((read_clock(address+1))/16)*10 + (read_clock(address+1))%16;if(KEY_Y == 0){item++;//数加 1}if(KEY_N== 0){item--;//数减 1}if(item>max) item=mini;//查看数值有效围if(item<mini) item=max;write_clock(0x8e,0x00);//允许写操作write_clock(address,(item/10)*16+item%10);//转换成16进制写入1302write_clock(0x8e,0x80);//写保护,禁止写操作// updata (); //刷新数据// lcm_w_mm();//刷新分}*/void DelayM(unsigned int a){//延时函数 1MS/次unsigned char i;while( --a != 0){for(i = 0; i < 125; i++); //一个 ; 表示空语句,CPU空转。
pyqt qtimer用法
pyqt qtimer用法
PyQtQTimer是一个提供计时器功能的库,可以在特定的时间间隔内执行一些操作。
以下是使用PyQt QTimer的一些基本用法:
1.导入PyQt库
要使用PyQt QTimer,必须先导入PyQt库。
可以使用以下代码导入PyQt库:
import PyQt5.QtCore
2.创建QTimer对象
要创建一个QTimer对象,可以使用以下代码:
timer = PyQt5.QtCore.QTimer()
3.设置定时器时间间隔
要设置定时器的时间间隔,可以使用以下代码:
timer.setInterval(1000) # 间隔时间为1秒
4.定义定时器的操作
要定义定时器的操作,可以使用以下代码:
def timer_callback():
print('Timer triggered')
5.将定时器的操作与定时器对象绑定
要将定时器的操作与定时器对象绑定,可以使用以下代码:
timer.timeout.connect(timer_callback)
6.启动定时器
要启动定时器,可以使用以下代码:
timer.start()
7.停止定时器
要停止定时器,可以使用以下代码:
timer.stop()
总之,PyQt QTimer是一个非常实用的库,可以在很多场合下进行计时器操作。
以上是一些基本用法的介绍,希望对大家有所帮助。
Qt绘制简单时钟
Qt绘制简单时钟本⽂实例为⼤家分享了Qt绘制简单时钟的具体代码,供⼤家参考,具体内容如下概述(1)基本原理利⽤简单的⼏何图形绘出表盘和指针,然后基于系统时间调整指针偏转⾓度达到模拟时钟的效果。
其中可以加上⾃⼰喜欢的背景资源,设计不同风格的时钟。
(2)部分函数说明①调⾊和填充painter.setBrush(Qt::red); //设置画刷,填充颜⾊(红⾊)painter.setPen(Qt::red); //设置画笔,外围轮廓颜⾊(红⾊)②抗锯齿(减少图形外围锯齿状,使线条平滑)painter.setRenderHint(QPainter::Antialiasing,true);③⾃动保存与恢复painter.save(); //保存属性painter.restore(); //属性恢复核⼼代码dialog.h⽂件class Dialog : public QDialog{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();void Dialog::paintEvent(QPaintEvent *); //函数声明private:Ui::Dialog *ui;};头⽂件#include <QPainter>#include <QPoint>#include <QTime>#include <QTimer>#include <QPixmap>#include <math.h>指针参数(第⼀个QPoint前可以加static)QPoint sec[4]={QPoint(0,-80),QPoint(2,0),QPoint(0,8),QPoint(-2,0)};QPoint min[4]={QPoint(0,-62),QPoint(4,0),QPoint(0,10),QPoint(-4,0)};QPoint hour[4]={QPoint(0,-48),QPoint(5,0),QPoint(0,11),QPoint(-5,0)};关联函数Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog){ui->setupUi(this);resize(300,300);QTimer * timer = new QTimer(this);timer->start(1000); //确定刷新时间(此处为1000毫秒=1秒)connect(timer,SIGNAL(timeout()),this,SLOT(update())); //update()为⾃动刷新界⾯,使指针可以不停转动}绘图函数(注意:只要时、分、秒三个指针任意完成⼀个,则其余两个只需修改指针旋转⾓度规则,其余属性相同即可)void Dialog::paintEvent(QPaintEvent *){QPainter painter(this);//时钟背景QRect q1(20,20,0.5*512,0.5*512);QRect q2(0,0,512,512);QPixmap map(":/b/ClockBack/clockback.png");painter.drawPixmap(q1,map,q2);QTime time=QTime::currentTime(); //调⽤系统时间//表盘painter.setRenderHint(QPainter::Antialiasing,true); //抗锯齿,平滑化painter.setPen(QPen(Qt::white,6,Qt::SolidLine)); //调⾊painter.translate(50,50);painter.drawEllipse(0,0,198,198);//指针属性painter.translate(99,99);//秒针painter.setRenderHint(QPainter::Antialiasing,true);painter.setBrush(Qt::red); //设置画刷,填充颜⾊painter.setPen(Qt::red); //设置画笔,外围轮廓颜⾊painter.save(); //保存属性painter.rotate(6.0*time.second());painter.drawPolygon(sec,4); //绘制指针(绘制⼏何图形函数)painter.restore(); //属性恢复//分针(同秒针)painter.setRenderHint(QPainter::Antialiasing,true);painter.setBrush(Qt::white);painter.setPen(Qt::white);painter.save();painter.rotate(6.0*time.minute()+time.second()/60.0);painter.drawPolygon(min,4);painter.restore();//时针(同秒针)painter.setRenderHint(QPainter::Antialiasing,true);painter.setBrush(Qt::black);painter.setPen(Qt::black);painter.save();painter.rotate(30.0*time.hour()+time.minute()/60.0);painter.drawPolygon(hour,4);painter.restore();//表盘刻度//长刻度(整点刻度)for(int i=1;i<=12;i++){painter.rotate(30);painter.drawLine(0,-100,0,-85);}//短刻度for(int i=1;i<=60;i++){painter.rotate(6);painter.drawLine(0,-94,0,-90);}}背景与效果图1、这⾥分享我⾃⼰采⽤的背景2、效果图以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本程序基于QWidget实现,可以动态显示钟表,并且可以添加铃声头文件widget.h代码如下:#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include<QTime>#include<QTimer>#include<QLabel>#include<QPushButton>#include<QVBoxLayout>#include<QHBoxLayout>#include<QTimeEdit>#include<QLCDNumber>#include<QVariant>#include<QAction>#include<QMenu>#include<QCloseEvent>#include<QApplication>#include<QMessageBox>#include<QSystemTrayIcon>namespace Ui {class Widget;}class Widget : public QWidget{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();signals:void isCurrentTime();private:Ui::Widget *ui;void init();void layout();void con();QLCDNumber *lcdNumber;QLabel *label;QTimeEdit *timeEdit;QPushButton *button;QTimer *timer;QTimer *uptimer;QTimer *dtimer;QTimer *itimer;QRect rect;int x;int y;double du;QSystemTrayIcon *trayIcon;QAction *restoreAct;QAction *quitAct;QMenu *trayIconMenu;private slots:void clockChange();void dialogUp();void mini();void down();void comToCur();protected:void closeEvent(QCloseEvent *e); };#endif // WIDGET_H源文件widget.cpp代码如下:#include "widget.h"#include "ui_widget.h"#include<QtDebug>#include<QApplication>#include<QDesktopWidget>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget){ui->setupUi(this);init();layout();con();void Widget::init(){setWindowTitle("我的闹钟");setFixedSize(300,80);timer=new QTimer;uptimer=new QTimer;dtimer=new QTimer;itimer=new QTimer;lcdNumber=new QLCDNumber;lcdNumber->setFixedSize(150,50);lcdNumber->setDigitCount(12);lcdNumber->setSegmentStyle(QLCDNumber::Flat);lcdNumber->display(QTime::currentTime().toString("hh:mm:ss:zzz"));label=new QLabel("闹钟");timeEdit=new QTimeEdit;timeEdit->setDisplayFormat("hh:mm:ss");button=new QPushButton("确定");trayIcon=new QSystemTrayIcon;trayIcon->setIcon(QIcon(":/new/prefix1/clock.ico"));trayIcon->show();setWindowIcon(QIcon(":/new/prefix1/clock.ico"));restoreAct=new QAction("显示",this);quitAct=new QAction("退出",this);trayIconMenu=new QMenu;trayIconMenu->addAction(restoreAct);trayIconMenu->addAction(quitAct);trayIcon->setContextMenu(trayIconMenu);setWindowFlags(Qt::WindowStaysOnTopHint);}void Widget::layout(){QHBoxLayout *rightTopLay=new QHBoxLayout;rightTopLay->addWidget(label);rightTopLay->addWidget(timeEdit);QHBoxLayout *rightButtonLay=new QHBoxLayout;rightButtonLay->addStretch();rightButtonLay->addWidget(button);QVBoxLayout *rightLay=new QVBoxLayout;rightLay->addLayout(rightTopLay);rightLay->addLayout(rightButtonLay);rightLay->addStretch();QHBoxLayout *m=new QHBoxLayout;m->addWidget(lcdNumber);m->addLayout(rightLay);setLayout(m);QDesktopWidget *p=new QDesktopWidget;p=QApplication::desktop();//rect=p->rect();x=rect.right()-width()-2;//y=rect.bottom()+30;//setGeometry(x,y,width(),height());}void Widget::con(){connect(timer,SIGNAL(timeout()),this,SLOT(clockChange()));timer->start(1);connect(uptimer,SIGNAL(timeout()),this,SLOT(dialogUp()));uptimer->start(20);connect(dtimer,SIGNAL(timeout()),this,SLOT(down()));dtimer->start(20);connect(button,SIGNAL(clicked(bool)),dtimer,SIGNAL(timeout())); connect(button,SIGNAL(clicked(bool)),this,SLOT(mini()));connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur()));itimer->start(20);connect(restoreAct,SIGNAL(triggered(bool)),uptimer,SIGNAL(timeout())) ;connect(this,SIGNAL(isCurrentTime()),uptimer,SIGNAL(timeout())); connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur()));itimer->start(20);connect(quitAct,SIGNAL(triggered(bool)),this,SLOT(close()));void Widget::clockChange(){lcdNumber->display(QTime::currentTime().toString("hh:mm:ss:zzz")); }void Widget::dialogUp(){show();uptimer->start(20);if(y>=rect.bottom()-height()-50){y-=2;}else {uptimer->stop();}setGeometry(x,y,width(),height());}void Widget::down(){dtimer->start(20);if(y<rect.bottom()){y+=height()+20;}else{dtimer->stop();}setGeometry(x,y,width(),height());}void Widget::mini(){hide();}void Widget::closeEvent(QCloseEvent *e){QMessageBox::information(0,"退出","您确定要退出吗?");void Widget::comToCur(){if(QVariant(QTime::currentTime().toString())==QVariant(timeEdit->time ()).toString()){QApplication::beep();emit isCurrentTime();}}Widget::~Widget(){delete ui;}程序运行截图:添加铃声:。