zoukankan      html  css  js  c++  java
  • 时钟Demo

      其实是一个很简单的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 gui
    10 
    11 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    12 
    13 TARGET = Clock
    14 TEMPLATE = app
    15 
    16 
    17 SOURCES += main.cpp 
    18     digtalwidget.cpp
    19 
    20 HEADERS  += 
    21     digtalwidget.h
    22 
    23 RESOURCES += 
    24     resource.qrc
    25 
    26 DISTFILES += 
    27     icon.rc
    28     
    29 RC_FILE += 
    30     icon.rc
      1 //digtalwidget.h(名字少打了一个i orz)
      2 
      3 #ifndef DIGTALWIDGET_H
      4 #define DIGTALWIDGET_H
      5 
      6 #include <QWidget>
      7 #include <QPoint>
      8 #include <QPalette>
      9 #include <QColor>
     10 #include <QTime>
     11 #include <QTimer>
     12 #include <QMouseEvent>
     13 #include <QDebug>
     14 #include <QLabel>
     15 #include <QGridLayout>
     16 #include <QMap>
     17 #include <QList>
     18 #include <QStringList>
     19 #include <QPixmap>
     20 #include <QHBoxLayout>
     21 #include <QIcon>
     22 #include <QPaintEvent>
     23 #include <QPainter>
     24 #include <QAction>
     25 #include <QBitmap>
     26 #include <QSettings>
     27 #include <QSystemTrayIcon>
     28 #include <QMenu>
     29 #include <QApplication>
     30 #include <QFile>
     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<QLabel *> 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<QAction *> 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<QPixmap> 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         else
    194             timeNumsLabelList[i]->setPixmap(numMan->cutStyle);
    195     }
    196 }
    197 
    198 void DigtalWidget::changeStyle()
    199 {
    200     QAction *catchAction = qobject_cast<QAction *>(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 <QApplication>
     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 }
  • 相关阅读:
    机器学习算法(SVM)公开课4月25日开讲
    手把手教你做文本挖掘
    ActiveReports公开课开启报名,学习如何解决中国式复杂报表难题
    DevExpress免费公开课,讲解即将发布的16.2新版功能
    Stimulsoft入门视频
    免费公开课,讲解强大的文档集成组件Aspose,现在可报名
    中国式商业智能报表ActiveReports免费公开课,10月20日开讲
    JavaScript图表FusionCharts免费在线公开课,由印度原厂技术工程师主讲,10月13日发车
    LoadRunner免费公开课,惠普金牌讲师亲授
    DevExpress VCL v16.1.3发布
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5734554.html
Copyright © 2011-2022 走看看