实现一个桌面工具
#ifndef WIDGET_H
#define WIDGET_H
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected :
// 在窗口上显示图片
void paintEvent(QPaintEvent *);
private:
Ui::Widget *ui;
QPoint p;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *){
QPainter p(this); //创建画家对象
p.begin(this);//指定当前窗口为绘图设备
p.drawPixmap(0, 0, QPixmap("../image/1.png")); // 显示图片
}
在xxx.pro文件中的末尾添加 RC_ICONS = xxxx.ico ,添加完成后运行项目
效果图如下:
#include "widget.h"
#include "ui_widget.h"
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//去窗口边框
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
//把窗口背景设置为透明
setAttribute(Qt::WA_TranslucentBackground);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *){
QPainter p(this); //创建画家对象
p.begin(this);//指定当前窗口为绘图设备
p.drawPixmap(0, 0, QPixmap("../image/1.png"));
}
在不管点击任何程序窗口都能使该程序窗口置于顶层可见。
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
#include "widget.h"
#include "ui_widget.h"
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//去窗口边框
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
//把窗口背景设置为透明
setAttribute(Qt::WA_TranslucentBackground);
// 窗口置顶
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *){
QPainter p(this); //创建画家对象
p.begin(this);//指定当前窗口为绘图设备
p.drawPixmap(0, 0, QPixmap("../image/1.png"));
}
以下可以实现,图片能被拖动到桌面的任意位置。
首先定义了三个变量和三个事件,分别用来记录鼠标操作的状态和窗口在桌面上不同的位置。
bool m_bDrag; // 判断拖拽状态
QPoint mouseStartPoint; // 鼠标起点坐标
QPoint windowTopLeftPoint; // 窗口左上角坐标
// 鼠标按下事件
void mousePressEvent(QMouseEvent *event);
// 鼠标移动事件
void mouseMoveEvent(QMouseEvent *event);
// 鼠标释放事件
void mouseReleaseEvent(QMouseEvent *event);
#ifndef WIDGET_H
#define WIDGET_H
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected :
//拖拽窗口
// 鼠标按下事件
void mousePressEvent(QMouseEvent *event);
// 鼠标移动事件
void mouseMoveEvent(QMouseEvent *event);
// 鼠标释放事件
void mouseReleaseEvent(QMouseEvent *event);
// 在窗口上显示图片
void paintEvent(QPaintEvent *);
private :
bool m_bDrag;
QPoint mouseStartPoint;
QPoint windowTopLeftPoint;
private:
Ui::Widget *ui;
QPoint p;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include
#include
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//去窗口边框
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
//把窗口背景设置为透明
setAttribute(Qt::WA_TranslucentBackground);
// 窗口置顶
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *){
QPainter p(this); //创建画家对象
p.begin(this);//指定当前窗口为绘图设备
p.drawPixmap(0, 0, QPixmap("../image/1.png"));
}
//拖拽操作
void Widget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_bDrag = true ;
//获得鼠标的初始位置
mouseStartPoint = event->globalPos();
//获得窗口的初始位置
windowTopLeftPoint = this ->frameGeometry().topLeft();
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (m_bDrag)
{
//获得鼠标移动的距离
QPoint distance = event->globalPos() - mouseStartPoint;
//改变窗口的位置
this ->move(windowTopLeftPoint + distance);
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_bDrag = false ;
}
}
托盘菜单有以下功能:
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
QSystemTrayIcon* mSysTrayIcon;
QMenu *mMenu;
QAction *mShowMainAction;
QAction *mExitAppAction;
QAction *mHideAppAction;
void createActions();
void createMenu();
protected :
//拖拽窗口
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *);
private :
bool m_bDrag;
QPoint mouseStartPoint;
QPoint windowTopLeftPoint;
private slots:
void on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason);
void on_showMainAction();
void on_exitAppAction();
private:
Ui::Widget *ui;
QPoint p;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#pragma execution_character_set("utf-8")
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowTitle("vvcat");
//去窗口边框
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
//把窗口背景设置为透明
setAttribute(Qt::WA_TranslucentBackground);
// 窗口置顶
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
mSysTrayIcon = new QSystemTrayIcon(this);
mSysTrayIcon->setIcon(QIcon(":/icon/VVCAT.ico"));
mSysTrayIcon->setToolTip(QObject::trUtf8("vvcat"));
connect(mSysTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));
connect(mSysTrayIcon,&QSystemTrayIcon::messageClicked,[&](){
this->show();
});
//建立托盘操作的菜单
createActions();
createMenu();
//在系统托盘显示此对象
mSysTrayIcon->show();
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *){
QPainter p(this); //创建画家对象
p.begin(this);//指定当前窗口为绘图设备
p.drawPixmap(0, 0, QPixmap("../image/1.gif"));
}
//拖拽操作
void Widget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_bDrag = true ;
//获得鼠标的初始位置
mouseStartPoint = event->globalPos();
//获得窗口的初始位置
windowTopLeftPoint = this ->frameGeometry().topLeft();
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (m_bDrag)
{
//获得鼠标移动的距离
QPoint distance = event->globalPos() - mouseStartPoint;
//改变窗口的位置
this ->move(windowTopLeftPoint + distance);
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_bDrag = false ;
}
}
void Widget::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
switch(reason){
case QSystemTrayIcon::Trigger:
mSysTrayIcon->showMessage(QObject::trUtf8("vvcat"),QObject::trUtf8("welcome use me"),QSystemTrayIcon::Information, 1000);
break;
case QSystemTrayIcon::DoubleClick:
this->show();
break;
default:
break;
}
}
void Widget::createActions()
{
mShowMainAction = new QAction(QObject::trUtf8("Show"), this);
connect(mShowMainAction,SIGNAL(triggered()),this,SLOT(on_showMainAction()));
mHideAppAction = new QAction(QObject::trUtf8("Hide"), this);
connect(mHideAppAction,&QAction::triggered,[&](){this->hide();});
mExitAppAction = new QAction(QObject::trUtf8("Quit"), this);
connect(mExitAppAction,SIGNAL(triggered()), this, SLOT(on_exitAppAction()));
}
void Widget::createMenu()
{
mMenu = new QMenu(this);
mMenu->addAction(mShowMainAction);
mMenu->addAction(mHideAppAction);
mMenu->addAction(mExitAppAction);
mSysTrayIcon->setContextMenu(mMenu);
}
void Widget::on_showMainAction()
{
this->show();
}
void Widget::on_exitAppAction()
{
exit(0);
}
开发未完,后续会继续更新…