qt桌面应用-闹钟提醒

前言

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

设计功能

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

搭建程序框架

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

0_1525653976357_0f62fc29-645d-45f6-9354-d4c6fba283b3-image.png

配置程序图标

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

IDI_ICON1               ICON    DISCARDABLE     "logo.ico"

0_1525655211549_logo.ico

背景透明

setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint | Qt::SubWindow | Qt::FramelessWindowHint);

    //背景透明
    setAttribute(Qt::WA_TranslucentBackground);

    //填充背景
    setAutoFillBackground(true);

    //画刷
    QPalette palette;
    QPixmap pixmap("qrc:/toumingBg.png");
    palette.setBrush(QPalette::Window, QBrush(pixmap));
    setPalette(palette);

    //qml透明
    ui->quickWidget->setClearColor(QColor(Qt::transparent));

效果,注意红边框

0_1525656018096_6c0390bf-7149-479c-883a-0f39ffeedda7-image.png

系统托盘

因为程序没有右上角关闭按钮,任务栏图标也去掉了,所以无法关闭程序。因此在系统托盘加个图标,象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();

0_1525656816896_4e28b28e-df21-4a58-892c-6b0a05f017d9-image.png

0_1525656838166_7c5b8040-475f-4fe1-a89a-d3fd282a126f-image.png

闪烁

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

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()
        }
    }

0_1525669493539_f80232ed-59be-4ade-b467-f67b1a9a8d23-image.png

注册开机启动

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

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

0_1525694527486_QQ截图20180507183633.png

福利

源代码下载
程序包下载,把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", "");
    }
}

0_1525738087123_f7d2d4fb-f454-4eb4-84a3-3bae1b400a45-image.png

总结

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

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

0_1525738661800_33a3c92d-0121-419c-9cb3-169705f82bba-image.png

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值