当前位置: 首页 > wzjs >正文

月坛网站建设公司益阳网站制作公司地址

月坛网站建设公司,益阳网站制作公司地址,网站建设与管理的过程,免费查公司的网站本专栏会不定时更新&#xff0c;如果有有趣的C代码或者编程可以在评论区留言&#xff0c;我会尽量满足粉丝的要求&#xff0c;同时还希望看到的朋友点个赞/收藏&#xff08;感谢/感谢&#xff09; 代码 main.cpp: #include "widget.h"#include <QApplication&g…

本专栏会不定时更新,如果有有趣的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_NAMESPACEenum RoleAct{Swing,Sleep,SayHello
};
Q_ENUM_NS(RoleAct)}using namespace  Act ;class Widget : public QWidget
{Q_OBJECTpublic: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博客

需要代码的可以评论区留言,私聊。

http://www.dtcms.com/wzjs/818356.html

相关文章:

  • 门户网站优化淘宝的网站建设怎么建
  • 网站站内站建设现状wordpress+设置七牛
  • 正规网站建设推荐谁好呢广州app制作开发
  • 搜索引擎营销的方法包括seo排名的方法
  • 怎么把WordPress和域名解绑网站建设 猫云seo
  • 做翻译 英文网站wordpress授权登录
  • 优设设计网站导航好的网站收入
  • 门户网站建设定制网站推广经理招聘
  • 坪山医院网站建设对于做网站有什么要求
  • 淘宝网站建设手机版全网营销系统是不是传销
  • wordpress整站数据帮助人做ppt的网站
  • 做网站怎么赚钱吗每一天做网站
  • 图片素材网站排行榜个人代做网站
  • 河北项目建设备案网站开淘宝店和做网站有什么区别
  • 网站备案幕布拍照张店网站建设公司
  • 没有网站可以做京东联盟吗长春网站建设排名
  • preec网站百度关键词排名爬虫
  • 深圳做营销网站的公司哪家好做3d图的网站有哪些软件
  • 百度网站怎么做视频wordpress 模版 摄影
  • 电子商务网站平台有哪些快速制作效果图软件
  • 深圳网站建设yuntianxia百度自然搜索排名优化
  • 30个做设计的网站贵州快速整站优化
  • 武义建设局官方网站内蒙古网络自学网站建设
  • 建设银行公积金网站提示udun大兴安岭网站制作
  • wordpress优化网站想建设网站
  • 知名的中文域名网站上海企业信息登记号查询
  • 重庆免费微网站建设wordpress传输失败
  • 临猗网站建设免费建网站模板
  • 雄安微网站开发免费一键生成短链接
  • Pk10网站建设多少钱做论坛和做网站有什么区别