diff --git a/README.md b/README.md index 5335661..88c7c0f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 休息休息 +# 休息一下 - 1. 通过qt,采用定时任务创建休息提示软件,默认一个小时提示一次 - 2. 随机从bing壁纸获取图片作为背景图 @@ -11,4 +11,8 @@ ## 打包 -- 1、软件直接生成exe在其他电脑上不可直接运行,因此需要用qt自带的windeployqt.exe工具搜集软件的依赖库,具体执行`..\windeployqt.exe dj_rest.exe`.qt中可能会有msvc以及mingw编译程序,采用哪个编译软件就用哪个下的windeployqt.exe +- 1、软件直接生成exe在其他电脑上不可直接运行,因此需要用qt自带的windeployqt.exe工具搜集软件的依赖库,具体执行`..\windeployqt.exe dj_rest.exe`.qt中可能会有msvc以及mingw编译程序,采用哪个编译软件就用哪个下的windeployqt.exe,如D:\Qt\Qt5.14.2\5.14.2\msvc2017_64\bin\windeployqt.exe. + + +## 细节 +- 1、托盘图标一直不显示是因为icon路径不对,需要先创建qt资源文件qrc,然后添加前缀,最后添加文件即可。使用方法通过前缀加上文件名称即可。 diff --git a/dj_rest.ini b/dj_rest.ini new file mode 100644 index 0000000..ed8e7ea --- /dev/null +++ b/dj_rest.ini @@ -0,0 +1,3 @@ +[config] +restTime=10 +workTime=60 diff --git a/dj_rest.pro b/dj_rest.pro index da4dbf4..dc5db6c 100644 --- a/dj_rest.pro +++ b/dj_rest.pro @@ -39,8 +39,13 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin DISTFILES += \ .gitignore \ README.md \ - favicon.ico + dj_rest.ini \ + favicon.ico \ + icon.png RC_ICONS = favicon.ico QT += network + +RESOURCES += \ + qt.qrc diff --git a/dj_rest.pro.user b/dj_rest.pro.user index 1ae9d08..ea0a97e 100644 --- a/dj_rest.pro.user +++ b/dj_rest.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -67,7 +67,7 @@ Desktop Qt 5.14.2 MSVC2017 64bit Desktop Qt 5.14.2 MSVC2017 64bit qt.qt5.5142.win64_msvc2017_64_kit - 0 + 1 0 0 @@ -119,14 +119,14 @@ 2 - D:/code/c/dj_rest + D:/code/c/build-dj_rest-Desktop_Qt_5_14_2_MSVC2017_64bit-Release true QtProjectManager.QMakeBuildStep false - true + false false true @@ -299,7 +299,7 @@ false true - D:/code/c/build-dj_rest-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug + D:/code/c/build-dj_rest-Desktop_Qt_5_14_2_MSVC2017_64bit-Release 1 diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..3165737 Binary files /dev/null and b/icon.png differ diff --git a/main.cpp b/main.cpp index fd3e533..53cbea7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,26 @@ #include "mainwindow.h" #include +#include int main(int argc, char *argv[]) { QApplication a(argc, argv); - MainWindow w; - w.show(); - return a.exec(); + QList screenList = QGuiApplication::screens(); + qDebug("screen size: %d;", screenList.size()); + QMainWindow window; + if (screenList.size() > 1) { + MainWindow w; + w.init(0); + w.show(); + MainWindow w2; + w2.init(1); + w2.show(); + return a.exec(); + } else { + MainWindow w; + w.init(0); + w.show(); + return a.exec(); + } } diff --git a/mainwindow.cpp b/mainwindow.cpp index 011b111..aefec0e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -2,6 +2,7 @@ #include #include #include + #include "mainwindow.h" #include "ui_mainwindow.h" @@ -9,14 +10,20 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { + settings = new QSettings(":/dj_rest.ini", QSettings::IniFormat); + int time1 = settings->value("config/restTime").toInt(); + if (time1 > 0 && time1 < 20 * 60) { + this->restTime = time1; + } + createTrayIcon(); ui->setupUi(this); + this->currentBackgroundColor = this->createRandomColor(); this->setWindowTitle("hava a rest"); - setWindowFlags(Qt::WindowStaysOnTopHint|Qt::WindowMaximizeButtonHint|Qt::WindowMinimizeButtonHint); - setWindowState(Qt::WindowMinimized); - this->init(); + setWindowFlags(Qt::WindowStaysOnTopHint|Qt::WindowMaximizeButtonHint|Qt::WindowMinimizeButtonHint|Qt::WindowCloseButtonHint); } -void MainWindow::init(){ +void MainWindow::init(uint screenId){ + this->screen = QGuiApplication::screens().at(screenId); _Timer = new QTimer(this); _Timer->setInterval(workTime); //一个小时 // _Timer->setInterval(1000 * 30); //30秒 @@ -41,16 +48,20 @@ void MainWindow::resting() { uint now = timeDate.toTime_t(); // 将当前时间转为时间戳 if ((now - this->restTimestamp) > restTime) { this->isResting = false; + this->changeLabel("工作中....."); setWindowState(Qt::WindowMinimized); - } - if (this->isResting) { + this->hide(); + } else { if (imageData.size() > 0) { this->setBackgroundImage(); } else { this->setBackgroundColor(); } - this->changeLabel(); + QString newTimeStr = this->refreshTime(); + this->changeLabel(newTimeStr); this->maxWindow(); + QRect rect = this->screen->availableGeometry(); + this->setGeometry(rect); } } } @@ -65,22 +76,15 @@ void MainWindow::timerFunc() { QDateTime timeDate = QDateTime::currentDateTime(); // 获取当前时间 this->restTimestamp = timeDate.toTime_t(); // 将当前时间转为时间戳 - // 随机选择是背景图片还是单一颜色,如果获取web图片失败则采用颜色背景 - int r = rand()%100; - qDebug("run time event! background type: %d; now: %d", r, this->restTimestamp); - this->currentBackgroundColor = this->createRandomColor(); - if (r > 5) { + try { ImageDownloader* imageDownloader = new ImageDownloader(); this->imageData = imageDownloader->getRandomImageData(); - try { - this->setBackgroundImage(); - } catch(_exception) { - qDebug("load image error"); - this->setBackgroundColor(); - } - } else { - this->setBackgroundColor(); + this->setBackgroundImage(); + } catch(_exception) { + qDebug("load image error"); + this->currentBackgroundColor = this->createRandomColor(); this->imageData = NULL; + this->setBackgroundColor(); } this->isResting = true; } @@ -118,15 +122,16 @@ void MainWindow::setBackgroundImage() { */ void MainWindow::maxWindow() { this->setFocus(); + this->show(); setWindowState(Qt::WindowMaximized);//初始状态最大化 } /** - * @brief MainWindow::changeLabel - * 修改label内容,休息时间内每秒更新一次时间 + * @brief MainWindow::refreshTime + * 更新休息倒计时 + * @return QString */ -void MainWindow::changeLabel() { - QLabel *label = this->ui->label; +QString MainWindow::refreshTime() { QDateTime timeDate = QDateTime::currentDateTime(); // 获取当前时间 uint nowTime = timeDate.toTime_t(); // 将当前时间转为时间戳 uint leftTime = restTime - (nowTime - restTimestamp); @@ -134,6 +139,15 @@ void MainWindow::changeLabel() { uint seconds = leftTime % 60; uint minutes = leftTime / 60; QString labelTxt = QString("该休息了, 站起来活动一下吧.休息剩余时间:%1m%2s").arg(minutes).arg(seconds); + return labelTxt; +} + +/** + * @brief MainWindow::changeLabel + * 修改label内容,休息时间内每秒更新一次时间 + */ +void MainWindow::changeLabel(QString labelTxt) { + QLabel *label = this->ui->label; label->setText(labelTxt); label->setStyleSheet("QLabel{background-color:rgb(255,255,255);}"); label->setMargin(10); @@ -141,6 +155,18 @@ void MainWindow::changeLabel() { label->adjustSize(); } + + +void MainWindow::changeEvent(QEvent *event) { + if (isMinimized()) { + if (this->tip_hide == false) { + this->hide(); + systemIcon->showMessage("have a rest", "后台运行", QSystemTrayIcon::MessageIcon::Information, 300); + this->tip_hide = true; + } + } +} + void MainWindow::resizeEvent(QResizeEvent *event) { if (this->isInit) { if (imageData.size() > 0) { @@ -153,6 +179,57 @@ void MainWindow::resizeEvent(QResizeEvent *event) { } } +/** + * @brief MainWindow::closeEvent + * 拦截close事件 + * @param event + */ +void MainWindow::closeEvent(QCloseEvent *event) { + setWindowState(Qt::WindowMinimized); + event->ignore(); +} + +/** + * @brief MainWindow::createTrayIcon + * 创建托盘 + */ +void MainWindow::createTrayIcon() { + systemIcon = new QSystemTrayIcon(this); + + systemIcon->setToolTip("这是系统系统图标"); // 设置提示语 + systemIcon->setIcon(QIcon(":/icon.png")); // 设置图标 + + // 增加托盘菜单 + QMenu *menu = new QMenu(); + QAction *closeAction = new QAction("关闭"); + menu->addAction(closeAction); + systemIcon->setContextMenu(menu); + connect(closeAction, SIGNAL(triggered(bool)), this, SLOT(close())); + systemIcon->show(); + // 关联点击拖盘事件 + connect(systemIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this, SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason))); + // 要在show之后调用 +// systemIcon->showMessage("have a rest", "程序正在后台运行",QSystemTrayIcon::MessageIcon::Information,500); +} + +/** + * @brief MainWindow::close + * 关闭程序 + */ +void MainWindow::close() { + QApplication::quit(); +} + +/** + * @brief MainWindow::on_activatedSysTrayIcon + * 点击托盘 + * @param reason + */ +void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason) { + if (reason == QSystemTrayIcon::Trigger) { + this->show(); + } +} /** * @brief MainWindow::createRandomColor diff --git a/mainwindow.h b/mainwindow.h index 85fc0b9..dc4bb2d 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,6 +4,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include "imagedownloader.h" @@ -18,16 +24,21 @@ class MainWindow : public QMainWindow public: MainWindow(QWidget *parent = nullptr); void resizeEvent(QResizeEvent *re); - void init(); + void changeEvent(QEvent *event); + void closeEvent(QCloseEvent *event); + void init(uint screenId); ~MainWindow(); + private slots: void timerFunc(); void resting(); + void close(); + void on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason); private: - uint restTime = 60 * 5; // 休息时间s + uint restTime = 10;//60 * 5; // 休息时间s int workTime = 1000 * 60 * 60; // 工作时间毫秒 bool isInit = false; bool isResting = false; @@ -35,13 +46,19 @@ private slots: QByteArray imageData; QColor currentBackgroundColor; Ui::MainWindow *ui; - QTimer* _Timer; + QTimer* _Timer; + QScreen* screen; + QSystemTrayIcon* systemIcon; + bool tip_hide = false; + QSettings *settings; void maxWindow(); QColor createRandomColor(); void setBackgroundColor(); void setBackgroundImage(); - void changeLabel(); + void changeLabel(QString labelTxt); + QString refreshTime(); + void createTrayIcon(); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 631e28e..0918933 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -19,7 +19,7 @@ 10 10 - 141 + 211 61 @@ -31,7 +31,7 @@ - 休息一下 + 工作中…… diff --git a/qt.qrc b/qt.qrc new file mode 100644 index 0000000..e34db96 --- /dev/null +++ b/qt.qrc @@ -0,0 +1,6 @@ + + + icon.png + dj_rest.ini + + diff --git a/ui_mainwindow.h b/ui_mainwindow.h index 94109a0..8dac0a4 100644 --- a/ui_mainwindow.h +++ b/ui_mainwindow.h @@ -31,14 +31,14 @@ class Ui_MainWindow { if (MainWindow->objectName().isEmpty()) MainWindow->setObjectName(QString::fromUtf8("MainWindow")); - MainWindow->resize(800, 600); + MainWindow->resize(600, 400); centralwidget = new QWidget(MainWindow); centralwidget->setObjectName(QString::fromUtf8("centralwidget")); label = new QLabel(centralwidget); label->setObjectName(QString::fromUtf8("label")); - label->setGeometry(QRect(290, 210, 141, 71)); + label->setGeometry(QRect(90, 20, 250, 71)); QFont font; - font.setPointSize(25); + font.setPointSize(20); font.setBold(true); font.setWeight(75); label->setFont(font); @@ -59,7 +59,7 @@ class Ui_MainWindow void retranslateUi(QMainWindow *MainWindow) { MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr)); - label->setText(QCoreApplication::translate("MainWindow", "\344\274\221\346\201\257\344\270\200\344\270\213", nullptr)); + label->setText(QCoreApplication::translate("MainWindow", "start working......", nullptr)); } // retranslateUi };