本专栏会不定时更新,如果有有趣的C++代码或者编程可以在评论区留言,我会尽量满足粉丝的要求,同时还希望看到的朋友点个赞/收藏(感谢/感谢)
代码
main.cpp:
#include "widget.h"
#include <QApplication>
#include<QSystemTrayIcon>
#include<QIcon>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QSystemTrayIcon sysTray(QIcon("://img/icon.png"),&w);
QMenu menu;
auto showAct=new QAction("show",&sysTray);
auto exitAct=new QAction("exit",&sysTray);
QObject::connect(showAct,&QAction::triggered,[&](){
w.setVisible(true);
});
QObject::connect(exitAct,&QAction::triggered,[&](){
QApplication::quit();
});
menu.addAction(showAct);
menu.addAction(exitAct);
sysTray.setContextMenu(&menu);
sysTray.show();
w.show();
return a.exec();
}
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QMap>
#include<QList>
#include<QUrl>
#include<QTimer>
#include<QEvent>
#include<QMouseEvent>
#include<QContextMenuEvent>
#include<QMenu>
class QPaintEvent;
namespace Act {
Q_NAMESPACE
enum RoleAct{
Swing,
Sleep,
SayHello
};
Q_ENUM_NS(RoleAct)
}
using namespace Act ;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public:
void showActAnimation(RoleAct k);
public slots:
void onMenuTriggered(QAction* action);
protected:
void paintEvent(QPaintEvent* event)override;
void contextMenuEvent(QContextMenuEvent* event);
private:
void loadRoleActRes();
void initMenu();
private:
QMap<RoleAct,QList<QUrl>> action_map;
QTimer* timer;
RoleAct cur_role_act;
QUrl cur_role_pix;
QMenu* menu;
};
class DragFilter:public QObject{
public:
bool eventFilter(QObject* obj,QEvent* event)
{
auto w=dynamic_cast<QWidget*>(obj);
if(!w)
return false;
if(event->type()==QEvent::MouseButtonPress)
{
auto e=dynamic_cast<QMouseEvent*>(event);
if(e)
{
pos=e->pos();
}
}
else if(event->type()==QEvent::MouseMove)
{
auto e=dynamic_cast<QMouseEvent*>(event);
if(e)
{
if(e->buttons()&Qt::MouseButton::LeftButton)
{
w->move(e->globalPos()-pos);
}
}
}
return QObject::eventFilter(obj,event);
}
private:
QPoint pos;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include<QPaintEvent>
#include<QPainter>//绘图
#include<QPixmap>//图片
#include<QCursor>
#include<QMetaEnum>
Widget::Widget(QWidget *parent)
: QWidget(parent),
timer(new QTimer(this)),
menu(new QMenu(this))
{
this->setWindowFlag(Qt::FramelessWindowHint);//去除窗口边框
this->setAttribute(Qt::WA_TranslucentBackground);//背景透明
this->installEventFilter(new DragFilter);
connect(timer,&QTimer::timeout,[this](){
static int index=0;//记录显示动作的当前图片索引
auto paths = this->action_map.value(this->cur_role_act);
this->cur_role_pix=paths[index++ % paths.size()];
//paintEvent() 不允许的
this->update();
});
initMenu();
loadRoleActRes();
showActAnimation(RoleAct::Swing);
}
Widget::~Widget() {}
void Widget::showActAnimation(RoleAct k)
{
timer->stop();
this->cur_role_act=k;
timer->start(60);
}
void Widget::onMenuTriggered(QAction *action)
{
QMetaEnum me=QMetaEnum::fromType<RoleAct>();
bool ok;
int k = me.keyToValue(action->text().toStdString().c_str(),&ok);
if(!ok)
return;
showActAnimation(static_cast<RoleAct>(k));
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPixmap pix;
pix.load(this->cur_role_pix.toLocalFile());
painter.drawPixmap(0,0,pix);
}
void Widget::contextMenuEvent(QContextMenuEvent *event)
{
this->menu->popup(QCursor::pos());
}
void Widget::loadRoleActRes()
{
auto addRes=[this](RoleAct k,QString path,int count)
{
QList<QUrl> paths;
char buf[260];
for (int i = 0; i < count; ++i) {
memset(buf, 0,sizeof(buf));
sprintf_s(buf,path.toStdString().c_str(),i);
paths.append(QUrl::fromLocalFile(buf));
}
action_map.insert(k,paths);
};
addRes(RoleAct::SayHello,"D:/001/pet/img/sayHello/sayHello_%d.png",28);
addRes(RoleAct::Swing,"D:/001/pet/img/swing/swing_%d.png",32);
addRes(RoleAct::Sleep,"D:/001/pet/img/sleep/sleep_%d.png",25);
}
void Widget::initMenu()
{
menu->addAction("SayHello");
menu->addAction("Sleep");
menu->addAction("Swing");
QAction* act=new QAction("Hide");
connect(act,&QAction::triggered,[this](){
this->setVisible(false);
});
menu->addAction(act);
connect(this->menu,&QMenu::triggered,this,&Widget::onMenuTriggered);
}
演示:
右键会出现四个选项:
点击相应图标会有不同动作。
sleep:
hello:
打包参考文章:Qt程序发布完整教程以及打包成exe文件【小白版】_qt发布可执行程序-CSDN博客
需要代码的可以评论区留言,私聊。