其实是一个很简单的Demo,可以编译了拿NSIS打包。最近在做富文本编辑器和补C++不记得的东西吧,项目遥遥无期。
//clock.pro
#-------------------------------------------------
#
# Project created by QtCreator --26T19::
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, ): QT += widgets
TARGET = Clock
TEMPLATE = app
SOURCES += main.cpp \
digtalwidget.cpp
HEADERS += \
digtalwidget.h
RESOURCES += \
resource.qrc
DISTFILES += \
icon.rc
RC_FILE += \
icon.rc
//digtalwidget.h(名字少打了一个i orz)
#ifndef DIGTALWIDGET_H
#define DIGTALWIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class DigtalWidget;
class NumberManger;
#define NUMSFIXEDWIDTH 30
#define NUMSFIXEDHEIGHT NUMSFIXEDWIDTH*2
#define FRAMEFIXWIDTH NUMSFIXEDWIDTH*11
#define FRAMEFIXHEIGHT NUMSFIXEDWIDTH*3
#define SETTING_FULLINFOM ".\\clocksetttings\\settings.ini"
#define REG_RUN "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
class DigtalWidget : public QWidget
{
Q_OBJECT
public:
explicit DigtalWidget(QWidget *parent = );
~DigtalWidget();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *);
private slots:
void showTimer();
void changeStyle();
void iconActivated(QSystemTrayIcon::ActivationReason reason);
void changeAutoStart();
void quitAndSet();
private:
void initItem();
void addRightMenu();
void createTrayMenu();
void createTrayIcon();
void writeSettings();
void readSettings();
void setAutoStart();
QPoint dragPositon;
QList<QLabel \*> timeNumsLabelList;
QHBoxLayout \*mainLayout;
NumberManger \*numMan;
QTimer \*numTimer;
QPixmap frameBackground;
QAction \*rightMenu\_Close,\*rightMenu\_Hide,\*trayMenu\_AutoStart,
\*trayMenu\_Close,\*trayMenu\_Normal, \*trayMenu\_Hide;
QList<QAction \*> rightMenu\_Style;
QMenu \*trayMenu;
QSystemTrayIcon \*trayIcon;
bool if\_auto\_start = false;
};
class NumberManger
{
friend class DigtalWidget;
public:
NumberManger();
NumberManger(const QString &);
void setStyle(const QString &);
void setStyle(QString &&);
private:
QStringList styleList{"Flat","Pink"};
QList
QPixmap cutStyle;
QString nowStyle = "Flat";
};
#endif // DIGTALWIDGET_H
//digtalwidget.cpp
#include "digtalwidget.h"
DigtalWidget::DigtalWidget(QWidget *parent)
: QWidget(parent)
{
//……………
move(, );//设定初始位置
initItem();
createTrayIcon();
readSettings();
frameBackground.load(tr(":/num/digitalNums/frame.png"),
,//默认加载模式
Qt::AvoidDither|Qt::ThresholdAlphaDither|Qt::ThresholdDither);
frameBackground = frameBackground.scaled(FRAMEFIXWIDTH,FRAMEFIXHEIGHT);
setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::Tool|Qt::X11BypassWindowManagerHint);
setAttribute(Qt::WA\_TranslucentBackground);
setWindowOpacity(0.95);//设置整体窗口透明度
setWindowIcon(QIcon(tr(":/images/clockIcon.png")));
setWindowTitle(tr("My Clock"));
setMask(frameBackground.mask());
//...............
addRightMenu();
setContextMenuPolicy(Qt::ActionsContextMenu);//开启右键菜单
showTimer();
resize(frameBackground.size());
}
void DigtalWidget::initItem()
{
numMan = new NumberManger("Flat");
numTimer = new QTimer(this);
connect(numTimer,SIGNAL(timeout()), this, SLOT(showTimer()));
numTimer->start();//1000ms启动计时器
for(int i = ;i < ;i++)
timeNumsLabelList << new QLabel("");
mainLayout = new QHBoxLayout(this);
mainLayout->addSpacing();
for(int i = ;i < ;i++)
mainLayout->addWidget(timeNumsLabelList\[i\]);
mainLayout->addSpacing();
mainLayout->setMargin();
}
void DigtalWidget::addRightMenu()
{
for(const auto &name : numMan->styleList)
{
QIcon icon(QObject::tr(":/num/digitalNums/") + name + tr("/rightMenuIcon.png"));
QString showName(name + tr(" style"));
QAction *tmp = new QAction(icon,showName,this);
rightMenu_Style << tmp;
addAction(tmp);
connect(tmp, SIGNAL(triggered()), this, SLOT(changeStyle()));
}
rightMenu\_Hide = new QAction(QIcon(":/pushButtons/pushButtons/showMinimized.png"),tr("隐藏"),this);
addAction(rightMenu\_Hide);
connect(rightMenu\_Hide,SIGNAL(triggered()),this,SLOT(hide()));
rightMenu\_Close = new QAction(QIcon(":/pushButtons/pushButtons/close.png"),tr("关闭"),this);
addAction(rightMenu\_Close);
connect(rightMenu\_Close, SIGNAL(triggered()),this,SLOT(quitAndSet()));
}
void DigtalWidget::createTrayIcon()
{
if(!QSystemTrayIcon::isSystemTrayAvailable())//如果不支持系统托盘图标就退出
return;
createTrayMenu();
trayIcon = new QSystemTrayIcon;
trayIcon->setIcon(QIcon(":/images/clockIcon.png"));
trayIcon->setToolTip(tr("Philip's clock V1.0"));
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
void DigtalWidget::createTrayMenu()
{
trayMenu = new QMenu(this);
trayMenu\_AutoStart = new QAction(tr("程序自启动"),this);
trayMenu->addAction(trayMenu\_AutoStart);
connect(trayMenu\_AutoStart,SIGNAL(triggered()),this,SLOT(changeAutoStart()));
trayMenu\_Hide = new QAction(QIcon(":/pushButtons/pushButtons/hide.png"),tr("隐藏"),this);
trayMenu->addAction(trayMenu\_Hide);
connect(trayMenu\_Hide,SIGNAL(triggered()),this,SLOT(hide()));
trayMenu\_Normal = new QAction(QIcon(":/pushButtons/pushButtons/normal.png"),tr("显示"),this);
trayMenu->addAction(trayMenu\_Normal);
connect(trayMenu\_Normal,SIGNAL(triggered()),this,SLOT(showNormal()));
trayMenu->addSeparator();//插一个分隔符
trayMenu\_Close = new QAction(QIcon(":/pushButtons/pushButtons/close.png"),tr("关闭"),this);
trayMenu->addAction(trayMenu\_Close);
connect(trayMenu\_Close, SIGNAL(triggered()),this,SLOT(quitAndSet()));//注意任务栏图标的关闭必须使用qApp的才能关
}
void DigtalWidget::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::DoubleClick:case QSystemTrayIcon::Trigger:
showNormal();
break;
case QSystemTrayIcon::MiddleClick:
trayIcon->showMessage(tr("From Philip:"),tr("Hi!"));
break;
default:
break;
}
}
void DigtalWidget::changeAutoStart()
{
if_auto_start = !if_auto_start;
setAutoStart();
}
void DigtalWidget::setAutoStart()
{
QString appName = QApplication::applicationName();
QSettings regSettings(REG_RUN,QSettings::NativeFormat);
if(!if\_auto\_start)//去除自启动信息
{
trayMenu\_AutoStart->setIcon(QIcon(""));
regSettings.remove(appName);
}
else//添加信息
{
QString appPath = QApplication::applicationFilePath();
regSettings.setValue(appName,appPath.replace("/","\\\\"));
trayMenu\_AutoStart->setIcon(QIcon(":/images/check.png"));
}
}
void DigtalWidget::writeSettings()
{
QSettings appBaseSettings(tr(SETTING_FULLINFOM),QSettings::IniFormat);//表示读取的是ini文件
appBaseSettings.setValue("/Geometry",saveGeometry());
appBaseSettings.setValue("/ifAutoRuning",if_auto_start);
appBaseSettings.setValue("/style",numMan->nowStyle);
}
void DigtalWidget::readSettings()
{
QFile file(tr(SETTING_FULLINFOM));
if(!file.open(QIODevice::ReadOnly))
return;//不存在配置文件
QSettings appBaseSettings(tr(SETTING\_FULLINFOM),QSettings::IniFormat);//表示读取的是ini文件
restoreGeometry(appBaseSettings.value("/Geometry").toByteArray());
if\_auto\_start = appBaseSettings.value("/ifAutoRuning").toBool();
setAutoStart();
numMan->nowStyle = appBaseSettings.value("/style").toString();
numMan->setStyle(numMan->nowStyle);
}
void DigtalWidget::quitAndSet()
{
writeSettings();
QApplication::quit();
}
void DigtalWidget::showTimer()
{
QTime time = QTime::currentTime();
QString text = time.toString("hh:mm:ss");
for(int i = ; i < text.count(); i++)
{
if(text\[i\] != ':')
{
int k = text\[i\].cell() - '';
timeNumsLabelList\[i\]->setPixmap(numMan->nums\[k\]);
}
else
timeNumsLabelList\[i\]->setPixmap(numMan->cutStyle);
}
}
void DigtalWidget::changeStyle()
{
QAction *catchAction = qobject_cast
auto name = catchAction->text();
name.replace(" style" , "", Qt::CaseSensitive);
numMan->setStyle(name);
showTimer();//强行刷新一次时间,去除延迟
}
void DigtalWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
dragPositon = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
void DigtalWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
move(event->globalPos() - dragPositon);
event->accept();
}
}
void DigtalWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawPixmap(,,frameBackground);
}
DigtalWidget::~DigtalWidget()
{
delete numMan;
}
NumberManger::NumberManger()
{
setStyle(nowStyle);
}
NumberManger::NumberManger(const QString &style)
{
setStyle(style);
}
void NumberManger::setStyle(const QString &style)
{
nums.clear();
for(int i = ;i < ;i++)
{
QString s(QObject::tr(":/num/digitalNums/") + style + QObject::tr("/%1.png").arg(i));
QPixmap m(s);
m = m.scaled(NUMSFIXEDWIDTH,NUMSFIXEDHEIGHT,Qt::KeepAspectRatio);
nums << m;
}
QString s(QObject::tr(":/num/digitalNums/") + style + QObject::tr("/cut.png"));
QPixmap m(s);
cutStyle = m.scaled((int)(NUMSFIXEDWIDTH/1.5),(int)(NUMSFIXEDHEIGHT/1.5));
nowStyle = style;
}
void NumberManger::setStyle(QString &&style)
{
nums.clear();
for(int i = ;i < ;i++)
{
QString s(QObject::tr(":/num/digitalNums/") + style + QObject::tr("/%1.png").arg(i));
QPixmap m(s);
m = m.scaled(NUMSFIXEDWIDTH,NUMSFIXEDHEIGHT,Qt::KeepAspectRatio);
nums << m;
}
QString s(QObject::tr(":/num/digitalNums/") + style + QObject::tr("/cut.png"));
QPixmap m(s);
cutStyle = m.scaled((int)(NUMSFIXEDWIDTH/1.5),(int)(NUMSFIXEDHEIGHT/1.5));
nowStyle = style;
}
//main.cpp
#include
#include "digtalwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DigtalWidget *widget = new DigtalWidget;
widget->show();
return a.exec();
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章