桌面闹钟提醒:qt桌面应用-闹钟提醒

前言

作为非专业业余程序员,天天做电脑面前很累。五一劳动节看了一篇帖子说,劳动时间越长,越放弃思考,为了放松自我,写一个桌面小程序,按时提醒自己休息

设计功能

启动程序开始计时,每50分钟提醒休息10分钟,这样一天8小时,能休息80分钟,想想就开心。

搭建程序框架

  • 新建项目,使用widget作为主窗口
  • 在widget里嵌入quickWidget作为主界面
  • 创建资源文件,main.qml文件
  • 设置quickWidget的source为main.qml

配置程序图标

在pro里写 RC_FILE += myapp.rc
myapp.rc文件内容

IDI_ICON1 ICON DISCARDABLE "logo.ico"

效果,注意红边框

系统托盘

因为程序没有右上角关闭按钮,任务栏图标也去掉了,所以无法关闭程序。因此在系统托盘加个图标,象qq那样,很不错的主意

trayIcon = new QSystemTrayIcon(); trayIcon->setIcon(QIcon(":/logo.ico")); QMenu *menu = new QMenu(); QAction *action = menu->addAction("退出"); connect(action, &QAction::triggered, this, &Widget::systemExit); trayIcon->setContextMenu(menu); trayIcon->show();

闪烁

正常窗口第一次显示时候会闪现出来,有背景存在所以不会察觉,但是我们的界面是透明,所以第一次闪烁非常非常明显,我没有找到好的方法解决这个问题,投机取巧了一下,效果出奇的好

setFixedSize(1,1);QTimer::singleShot(1000, this, &Widget::init);void Widget::init(){ setWindowState(Qt::WindowMaximized);}

全屏黑屏

设置窗口全屏后黑屏,进行下面设置发现不黑屏,原因不明,全屏有毒。。。

void Widget::init(){ setWindowState(Qt::WindowMaximized); setGeometry(0,0,QApplication::desktop()->width(),QApplication::desktop()->height()+1);}

接下来终于到了实现业务逻辑的部分了,真不容易。

  • timer实现计时,达到50分钟后显示窗口,黑布下滑遮盖桌面,
  • 动画结束后,timerMin计时10分钟内时间流逝
  • 10分钟过去后,黑布上拉,重新开始timer计时
  • 动画结束,窗口隐藏
Timer{ id: timer interval: 1000 repeat: true running: true onTriggered: { var currentDate = new Date //秒到达50*60 if(currentDate-lastDate > workTime*1000){ widget.show() animDown.restart() } } } ParallelAnimation{ id: animDown PropertyAnimation{ target: rect; property: "y"; from: -Screen.height; to: 0; easing.type: Easing.Linear; duration: 500} onStopped: { lastMinDate = new Date myTime = myTimeText timerMin.restart() timer.stop() } } Timer{ id: timerMin interval: 500 repeat: true onTriggered: { var currentDate = new Date //流逝的时间 var timeLapse = Math.floor(currentDate-lastMinDate) / 1000 if(restTime-timeLapse>=0){ var left = (restTime-timeLapse) var min = Number(left/60).toFixed(0) var sec = Number(left%60).toFixed(0) myTime = addZero(min) + ":" + addZero(sec) } else{ lastDate = currentDate timerMin.stop() animUp.restart() timer.restart() } } } ParallelAnimation{ id: animUp PropertyAnimation{ target: rect; property: "y"; from: 0; to: -Screen.height; easing.type: Easing.Linear; duration: 500} onStopped: { widget.hide() } }

注册开机启动

花了一下午时间,经历了N多坑

  • QSettings设置注册表好简单啊——然并软
  • Win Api有啥方法——RegOpenKeyEx,RegSetValueEx,RegCloseKey
  • 为啥编译失败——该api需要添加lib
  • 为啥windows.h不好用——可能需要qt_windows.h
  • QString咋转LPCWSTR——TEXT()
  • 都写入成功了,为啥看不到——需要添加管理员权限
  • 普通注册表为啥写入乱码啊,unicode编码转成ANSI编码再转LPBYTE
  • 值终于对了,应用名怎么只剩一个字母了——QString转LPCSTR
  • 开机启动写成功了,注册表怎么还没有——开机启动项有就行了

福利

源代码下载
程序包下载,把bin文件夹下载下来哦

第二天

注册开机启动,QSettings其实好用,

void Widget::autoRun(bool run){ QString path = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; QString exePath = QApplication::applicationFilePath(); exePath = exePath.replace("/", "\\"); QSettings *reg = new QSettings(path, QSettings::NativeFormat); if(run){ reg->setValue("rest", exePath); } else{ reg->setValue("rest", ""); }}

总结

qt下修改注册表,推荐用QSettings,很方便

  • 64位系统有重定向问题,真正的默认开机启动项在
    HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run

相关推荐

相关文章