《基于Qt5的闹钟实现代码.docx》由会员分享,可在线阅读,更多相关《基于Qt5的闹钟实现代码.docx(6页珍藏版)》请在三一办公上搜索。
1、基于Qt5的闹钟实现代码本程序基于QWidget实现,可以动态显示钟表,并且可以添加铃声 头文件widget.h代码如下: #ifndef WIDGET_H #define WIDGET_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Ui class Widget; class Widget : public QWidget Q_O
2、BJECT public: 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 rec
3、t; 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
4、 #include ui_widget.h #include #include #include 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
5、=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(确定); trayIco
6、n=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-se
7、tContextMenu(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); Q
8、VBoxLayout *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=r
9、ect.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); connec
10、t(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(timeou
11、t); 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
12、uptimer-stop; setGeometry(x,y,width,height); void Widget:down dtimer-start(20); if(ystop; 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; 程序运行截图: 添加铃声: