博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
时钟Demo
阅读量:5064 次
发布时间:2019-06-12

本文共 12052 字,大约阅读时间需要 40 分钟。

  其实是一个很简单的Demo,可以编译了拿NSIS打包。最近在做富文本编辑器和补C++不记得的东西吧,项目遥遥无期。

1 //clock.pro 2  3 #------------------------------------------------- 4 # 5 # Project created by QtCreator 2016-07-26T19:06:54 6 # 7 #------------------------------------------------- 8  9 QT       += core gui10 11 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets12 13 TARGET = Clock14 TEMPLATE = app15 16 17 SOURCES += main.cpp \18     digtalwidget.cpp19 20 HEADERS  += \21     digtalwidget.h22 23 RESOURCES += \24     resource.qrc25 26 DISTFILES += \27     icon.rc28     29 RC_FILE += \30     icon.rc
1 //digtalwidget.h(名字少打了一个i orz)  2   3 #ifndef DIGTALWIDGET_H  4 #define DIGTALWIDGET_H  5   6 #include 
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 #include
23 #include
24 #include
25 #include
26 #include
27 #include
28 #include
29 #include
30 #include
31 32 class DigtalWidget; 33 class NumberManger; 34 35 #define NUMSFIXEDWIDTH 30 36 #define NUMSFIXEDHEIGHT NUMSFIXEDWIDTH*2 37 #define FRAMEFIXWIDTH NUMSFIXEDWIDTH*11 38 #define FRAMEFIXHEIGHT NUMSFIXEDWIDTH*3 39 #define SETTING_FULLINFOM ".\\clocksetttings\\settings.ini" 40 #define REG_RUN "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" 41 42 class DigtalWidget : public QWidget 43 { 44 Q_OBJECT 45 public: 46 explicit DigtalWidget(QWidget *parent = 0); 47 ~DigtalWidget(); 48 protected: 49 void mousePressEvent(QMouseEvent *event); 50 void mouseMoveEvent(QMouseEvent *event); 51 void paintEvent(QPaintEvent *); 52 private slots: 53 void showTimer(); 54 void changeStyle(); 55 void iconActivated(QSystemTrayIcon::ActivationReason reason); 56 void changeAutoStart(); 57 void quitAndSet(); 58 private: 59 void initItem(); 60 void addRightMenu(); 61 void createTrayMenu(); 62 void createTrayIcon(); 63 void writeSettings(); 64 void readSettings(); 65 void setAutoStart(); 66 67 QPoint dragPositon; 68 QList
timeNumsLabelList; 69 QHBoxLayout *mainLayout; 70 NumberManger *numMan; 71 QTimer *numTimer; 72 QPixmap frameBackground; 73 QAction *rightMenu_Close,*rightMenu_Hide,*trayMenu_AutoStart, 74 *trayMenu_Close,*trayMenu_Normal, *trayMenu_Hide; 75 QList
rightMenu_Style; 76 77 QMenu *trayMenu; 78 QSystemTrayIcon *trayIcon; 79 80 bool if_auto_start = false; 81 }; 82 83 class NumberManger 84 { 85 friend class DigtalWidget; 86 public: 87 NumberManger(); 88 NumberManger(const QString &); 89 void setStyle(const QString &); 90 void setStyle(QString &&); 91 92 private: 93 QStringList styleList{ "Flat","Pink"}; 94 QList
nums; 95 QPixmap cutStyle; 96 QString nowStyle = "Flat"; 97 }; 98 99 100 #endif // DIGTALWIDGET_H
1 //digtalwidget.cpp  2   3 #include "digtalwidget.h"  4   5 DigtalWidget::DigtalWidget(QWidget *parent)   6     : QWidget(parent)  7 {  8     //...............  9     move(1552, 82);//设定初始位置 10     initItem(); 11     createTrayIcon(); 12     readSettings(); 13      14     frameBackground.load(tr(":/num/digitalNums/frame.png"), 15                          0,//默认加载模式 16                          Qt::AvoidDither|Qt::ThresholdAlphaDither|Qt::ThresholdDither); 17     frameBackground = frameBackground.scaled(FRAMEFIXWIDTH,FRAMEFIXHEIGHT); 18      19     setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::Tool|Qt::X11BypassWindowManagerHint); 20     setAttribute(Qt::WA_TranslucentBackground); 21     setWindowOpacity(0.95);//设置整体窗口透明度 22     setWindowIcon(QIcon(tr(":/images/clockIcon.png"))); 23     setWindowTitle(tr("My Clock")); 24     setMask(frameBackground.mask()); 25     //............... 26     addRightMenu(); 27     setContextMenuPolicy(Qt::ActionsContextMenu);//开启右键菜单 28  29     showTimer(); 30     resize(frameBackground.size()); 31 } 32  33 void DigtalWidget::initItem() 34 { 35     numMan = new NumberManger("Flat"); 36     numTimer = new QTimer(this); 37     connect(numTimer,SIGNAL(timeout()), this, SLOT(showTimer())); 38     numTimer->start(1000);//1000ms启动计时器 39      40     for(int i = 0;i < 8;i++) 41         timeNumsLabelList << new QLabel(""); 42      43     mainLayout = new QHBoxLayout(this); 44     mainLayout->addSpacing(18); 45     for(int i = 0;i < 8;i++) 46         mainLayout->addWidget(timeNumsLabelList[i]); 47     mainLayout->addSpacing(25); 48     mainLayout->setMargin(10); 49 } 50  51 void DigtalWidget::addRightMenu() 52 { 53     for(const auto &name : numMan->styleList) 54     { 55         QIcon icon(QObject::tr(":/num/digitalNums/") + name + tr("/rightMenuIcon.png")); 56         QString showName(name + tr(" style")); 57         QAction *tmp = new QAction(icon,showName,this); 58         rightMenu_Style << tmp; 59         addAction(tmp); 60         connect(tmp, SIGNAL(triggered()), this, SLOT(changeStyle())); 61     } 62      63     rightMenu_Hide = new QAction(QIcon(":/pushButtons/pushButtons/showMinimized.png"),tr("隐藏"),this); 64     addAction(rightMenu_Hide); 65     connect(rightMenu_Hide,SIGNAL(triggered()),this,SLOT(hide())); 66      67     rightMenu_Close = new QAction(QIcon(":/pushButtons/pushButtons/close.png"),tr("关闭"),this); 68     addAction(rightMenu_Close); 69     connect(rightMenu_Close, SIGNAL(triggered()),this,SLOT(quitAndSet())); 70 } 71  72 void DigtalWidget::createTrayIcon() 73 { 74     if(!QSystemTrayIcon::isSystemTrayAvailable())//如果不支持系统托盘图标就退出 75         return; 76     createTrayMenu(); 77     trayIcon = new QSystemTrayIcon; 78     trayIcon->setIcon(QIcon(":/images/clockIcon.png")); 79      80     trayIcon->setToolTip(tr("Philip's clock V1.0")); 81     trayIcon->setContextMenu(trayMenu); 82     trayIcon->show(); 83      84     connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)), 85             this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); 86 } 87  88 void DigtalWidget::createTrayMenu() 89 { 90     trayMenu = new QMenu(this); 91      92     trayMenu_AutoStart = new QAction(tr("程序自启动"),this); 93     trayMenu->addAction(trayMenu_AutoStart); 94     connect(trayMenu_AutoStart,SIGNAL(triggered()),this,SLOT(changeAutoStart())); 95      96     trayMenu_Hide = new QAction(QIcon(":/pushButtons/pushButtons/hide.png"),tr("隐藏"),this); 97     trayMenu->addAction(trayMenu_Hide); 98     connect(trayMenu_Hide,SIGNAL(triggered()),this,SLOT(hide())); 99     100     trayMenu_Normal = new QAction(QIcon(":/pushButtons/pushButtons/normal.png"),tr("显示"),this);101     trayMenu->addAction(trayMenu_Normal);102     connect(trayMenu_Normal,SIGNAL(triggered()),this,SLOT(showNormal()));103     104     trayMenu->addSeparator();//插一个分隔符105     106     trayMenu_Close = new QAction(QIcon(":/pushButtons/pushButtons/close.png"),tr("关闭"),this);107     trayMenu->addAction(trayMenu_Close);108     connect(trayMenu_Close, SIGNAL(triggered()),this,SLOT(quitAndSet()));//注意任务栏图标的关闭必须使用qApp的才能关109 }110 111 void DigtalWidget::iconActivated(QSystemTrayIcon::ActivationReason reason)112 {113     switch (reason) 114     {115     case QSystemTrayIcon::DoubleClick:case QSystemTrayIcon::Trigger:116         showNormal();117         break;118     case QSystemTrayIcon::MiddleClick:119         trayIcon->showMessage(tr("From Philip:"),tr("Hi!"));120         break;121     default:122         break;123     }124 }125 126 void DigtalWidget::changeAutoStart()127 {128     if_auto_start = !if_auto_start;129     setAutoStart();130 }131 132 void DigtalWidget::setAutoStart()133 {134     QString appName = QApplication::applicationName();135     QSettings regSettings(REG_RUN,QSettings::NativeFormat);136     137     if(!if_auto_start)//去除自启动信息138     {139         trayMenu_AutoStart->setIcon(QIcon(""));140         regSettings.remove(appName);141     }142     else//添加信息143     {144         QString appPath = QApplication::applicationFilePath();145         regSettings.setValue(appName,appPath.replace("/","\\"));146         trayMenu_AutoStart->setIcon(QIcon(":/images/check.png"));147     }148 }149 150 void DigtalWidget::writeSettings()151 {152     QSettings appBaseSettings(tr(SETTING_FULLINFOM),QSettings::IniFormat);//表示读取的是ini文件153     appBaseSettings.setValue("/Geometry",saveGeometry());154     appBaseSettings.setValue("/ifAutoRuning",if_auto_start);155     appBaseSettings.setValue("/style",numMan->nowStyle);156 }157 158 void DigtalWidget::readSettings()159 {160     QFile file(tr(SETTING_FULLINFOM));161     if(!file.open(QIODevice::ReadOnly))162         return;//不存在配置文件163     164     QSettings appBaseSettings(tr(SETTING_FULLINFOM),QSettings::IniFormat);//表示读取的是ini文件165     166     restoreGeometry(appBaseSettings.value("/Geometry").toByteArray());167     168     if_auto_start = appBaseSettings.value("/ifAutoRuning").toBool();169     setAutoStart();170     171     numMan->nowStyle = appBaseSettings.value("/style").toString();172     numMan->setStyle(numMan->nowStyle);173 }174 175 void DigtalWidget::quitAndSet()176 {177     writeSettings();178     QApplication::quit();179 }180 181 void DigtalWidget::showTimer()182 {183     QTime time = QTime::currentTime();184     QString text = time.toString("hh:mm:ss");185     186     for(int i = 0; i < text.count(); i++)187     {188         if(text[i] != ':')189         {190             int k = text[i].cell() - '0';191             timeNumsLabelList[i]->setPixmap(numMan->nums[k]);192         }193         else194             timeNumsLabelList[i]->setPixmap(numMan->cutStyle);195     }196 }197 198 void DigtalWidget::changeStyle()199 {200     QAction *catchAction = qobject_cast
(sender());201 auto name = catchAction->text();202 name.replace(" style" , "", Qt::CaseSensitive);203 numMan->setStyle(name);204 showTimer();//强行刷新一次时间,去除延迟205 }206 207 void DigtalWidget::mousePressEvent(QMouseEvent *event)208 {209 if(event->button() == Qt::LeftButton) 210 {211 dragPositon = event->globalPos() - frameGeometry().topLeft();212 event->accept();213 }214 }215 216 void DigtalWidget::mouseMoveEvent(QMouseEvent *event)217 {218 if(event->buttons() & Qt::LeftButton)219 {220 move(event->globalPos() - dragPositon);221 event->accept();222 }223 }224 225 void DigtalWidget::paintEvent(QPaintEvent *)226 {227 QPainter painter(this);228 painter.drawPixmap(0,0,frameBackground);229 }230 231 DigtalWidget::~DigtalWidget()232 {233 delete numMan;234 }235 236 NumberManger::NumberManger()237 {238 setStyle(nowStyle);239 }240 241 NumberManger::NumberManger(const QString &style)242 {243 setStyle(style);244 }245 246 void NumberManger::setStyle(const QString &style)247 {248 nums.clear();249 for(int i = 0;i < 10;i++)250 {251 QString s(QObject::tr(":/num/digitalNums/") + style + QObject::tr("/%1.png").arg(i));252 QPixmap m(s);253 m = m.scaled(NUMSFIXEDWIDTH,NUMSFIXEDHEIGHT,Qt::KeepAspectRatio);254 nums << m;255 }256 QString s(QObject::tr(":/num/digitalNums/") + style + QObject::tr("/cut.png"));257 QPixmap m(s);258 cutStyle = m.scaled((int)(NUMSFIXEDWIDTH/1.5),(int)(NUMSFIXEDHEIGHT/1.5));259 nowStyle = style;260 }261 262 void NumberManger::setStyle(QString &&style)263 {264 nums.clear();265 for(int i = 0;i < 10;i++)266 {267 QString s(QObject::tr(":/num/digitalNums/") + style + QObject::tr("/%1.png").arg(i));268 QPixmap m(s);269 m = m.scaled(NUMSFIXEDWIDTH,NUMSFIXEDHEIGHT,Qt::KeepAspectRatio);270 nums << m;271 }272 QString s(QObject::tr(":/num/digitalNums/") + style + QObject::tr("/cut.png"));273 QPixmap m(s);274 cutStyle = m.scaled((int)(NUMSFIXEDWIDTH/1.5),(int)(NUMSFIXEDHEIGHT/1.5));275 nowStyle = style;276 }
1 //main.cpp 2  3 #include 
4 #include "digtalwidget.h" 5 6 int main(int argc, char *argv[]) 7 { 8 QApplication a(argc, argv); 9 DigtalWidget *widget = new DigtalWidget;10 11 widget->show();12 13 return a.exec();14 }

 

转载于:https://www.cnblogs.com/Philip-Tell-Truth/p/5734554.html

你可能感兴趣的文章
Market entry case
查看>>
bzoj1230 开关灯 线段树
查看>>
LinearLayout
查看>>
css3动画属性
查看>>
Mongodb 基本命令
查看>>
控制文件的备份与恢复
查看>>
PHP的SQL注入技术实现以及预防措施
查看>>
软件目录结构规范
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android入门之文件系统操作(二)文件操作相关指令
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
jdk从1.8降到jdk1.7失败
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>