此程序可实现像qq界面那样在桌面窗口边界隐藏和出现;
并且可以往该程序界面里将桌面文件或软件拖进去,实现路径的保存,双击界面上图标可以在桌面上打开,可以方便桌面的管理与优化整洁;对于有强迫症朋友可以试下,还有对于代码里有所雷同的代码已经注明出处,对于可以优化的代码,请指教,谢谢;相互学习,共同进步;
main.cpp
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
atuohidewidget w;
w.show();
return a.exec();
}
atuohidewidget.h
#pragma once
#include
#include "ui_atuohidewidget.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "buttonclicked.h"
#include "path.h"
#include
class atuohidewidget : public QWidget
{
Q_OBJECT
public:
atuohidewidget(QWidget *parent = Q_NULLPTR);
virtual void paintEvent(QPaintEvent *event);
void enterEvent(QEvent*event);//鼠标进入事件
void leaveEvent(QEvent *event);//鼠标离开事件
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent*event);
void mouseReleaseEvent(QMouseEvent*event);
void hidewidget();
void dragEnterEvent(QDragEnterEvent *event);//鼠标拖动图标进入程序窗口内
void dropEvent(QDropEvent*event);//鼠标松开
private slots://所用信号槽
void close_buttton_clicked();//单击关闭按钮
void input_path(QString path);
void reload();
/*void mouseDoubleClickEvent(QMouseEvent*event);*/
// void dragLeaveEvent(QDragLeaveEvent *event);
private:
Ui::atuohidewidgetClass ui;
bool is_hiding;
QPoint m_startPosition, m_framePosition;
bool m_dragging;
QPixmap m_back;
buttonclicked*button;
int count=0;
path m_path;
QFileIconProvider icon_provider;
};
atuohidewidget.cpp
#include "atuohidewidget.h"
#include
atuohidewidget::atuohidewidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
m_path.load();//当运行进程时,加载路径,即解析xml文件路径数据存储在m_choosed_path容器中,以及赋值m_count;
reload();//即重新加载文件图标和按钮进窗口;
setAcceptDrops(true);
// button = new QPushButton(this);
// button->setIcon(icon);
// button->setIconSize(QSize(23,23));
// button->setGeometry(QRect(100, 100, 100, 100));
// setDragDropMode(QAbstractItemView::DragDrop);
// setDragEnabled(true);
// setDropIndicatorShown(true);
m_dragging = false;//判断是否有拖动程序窗口;
is_hiding = false;//判断程序窗口是否隐藏;
//不显示标题栏和边框
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
//设置背景(全透明)
setAttribute(Qt::WA_TranslucentBackground);
QPixmap close = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);//样式表标准样式;
ui.closebutton->setIcon(close);//设置按钮图标和大小;
ui.closebutton->setGeometry(QRect(215, 2, 25, 25));
m_back.load(":/atuohidewidget/Resources/sucai3.jpg");//背景;
connect(ui.closebutton, SIGNAL(clicked()), this, SLOT(close_buttton_clicked()));
}
void atuohidewidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QRect frameRect = rect();
/*frameRect.adjust(1, 1, -1, -1);*/
// painter.setPen(QColor(0, 0,0));
// painter.drawRoundRect(frameRect, 10, 10);
painter.drawPixmap(frameRect, m_back);
}
void atuohidewidget::enterEvent(QEvent*event)
{
int screenwidth = QApplication::desktop()->width();//鼠标进入程序窗口时,获取桌面长宽;
int screenhight = QApplication::desktop()->height();
if (is_hiding)
{
QRect erect = QRect(this->pos(), this->size());//获取当前程序窗口位置坐标;
QPropertyAnimation*animation = new QPropertyAnimation(this, "geometry");//设置窗口动画效果;
animation->setDuration(500);//动画效果时间;
animation->setStartValue(erect);//动画初始位置;
QRect enrect;//下面是计算动画end位置值并赋给enrect,我用的方法计较简单,见笑啦。。;
// if (erect.bottom()>screenhight)
// {
// enrect = QRect(this->x(),(screenhight-this->height())+1, this->size().width(), this->rect().height());
// }
if (erect.right()>screenwidth/*||(erect.right()>screenwidth&&erect.top()<0)*/)
{
enrect = QRect((screenwidth - this->width())+2, this->y(), this->size().width(), this->rect().height());
}
if (erect.left()<0 /*|| (erect.left()<0 && erect.top()<0)*/)
{
enrect = QRect(-1, this->y(), this->size().width(), this->rect().height());
}
if (erect.top()<0&&erect.left()>0&&erect.right()x(), -1, this->size().width(), this->rect().height());
}
animation->setEndValue(enrect);//设置动画end值;
animation->start();
is_hiding = false;//解除隐藏状态;
}
}
void atuohidewidget::leaveEvent(QEvent*event)//判断下拖动程序窗口是否处于桌面窗口边界内;
{
int screenwidth = QApplication::desktop()->width();
int screenhight = QApplication::desktop()->height();
QRect erect = QRect(this->pos(), this->size());
if (erect.top() < 0 || erect.left() < 0 | erect.right()>screenwidth /*| erect.bottom()>screenhight*/)
{
is_hiding = true;
hidewidget();//此函数为前面动画的逆向动画效果;
}
else
{
return;
}
}
void atuohidewidget::hidewidget()
{
int screenwidth = QApplication::desktop()->width();
int screenhight = QApplication::desktop()->height();
QRect erect = QRect(this->pos(), this->size());
QPropertyAnimation*animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(500);
animation->setStartValue(erect);
QRect enrect;
if (erect.top() < 0)
{
enrect = QRect(this->x(), -this->height() + 2, this->size().width(), this->rect().height());
}
if (erect.left() < 0)
{
enrect = QRect(-this->width() + 2, this->y(), this->size().width(), this->rect().height());
}
if (erect.right() > screenwidth)
{
enrect = QRect(screenwidth-2, this->y(), this->size().width(), this->rect().height());
}
// if (erect.bottom() > screenhight)
// {
// enrect = QRect(this->x(), screenhight - 2, this->size().width(), this->rect().height());
// }
animation->setEndValue(enrect);
animation->start();
}
void atuohidewidget::mouseMoveEvent(QMouseEvent*event)//以下三个函数为拖动程序窗口移动;
{
// 只响应左键
if (event->buttons() & Qt::LeftButton)
{
if (m_dragging)
{
// delta 相对偏移量,
QPoint delta = event->globalPos() - m_startPosition;
// 新位置:窗体原始位置 + 偏移量
move(m_framePosition + delta);
}
/*QWidget::mouseMoveEvent(event);*/
}
}
void atuohidewidget::mousePressEvent(QMouseEvent*event)
{
// 只响应左键
if (event->button() == Qt::LeftButton)
{
QRect titleRect = rect();
/*titleRect.setBottom(titleRect.top() + 30);*/
if (titleRect.contains(event->pos()))
{
m_dragging = true;
m_startPosition = event->globalPos();
m_framePosition = frameGeometry().topLeft();
}
}
}
void atuohidewidget::mouseReleaseEvent(QMouseEvent * event)
{
m_dragging = false;
/*QWidget::mouseReleaseEvent(event);*/
}
void atuohidewidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls())//如果拖动事件有资源定位器
event->acceptProposedAction();//Accepts the drag event with the proposedAction;
/*QString currentPath = QCoreApplication::applicationDirPath();*/
/*int i = 5;*/
}
void atuohidewidget::dropEvent(QDropEvent*event)
{
if (event->mimeData()->hasUrls())
{
count++;//拖动文件数量+1;
QUrl t = event->mimeData()->urls()[0];
QFileInfo info(t.toLocalFile());
QString path = info.absoluteFilePath();//将所拖动文件路径赋值给path;
if (info.isSymLink())
{
path = info.symLinkTarget();//绝对路径赋值给path;
}
input_path(path);
/*QString file_name = url.toLocalFile();*/
/*QString file_path = url.fileName();*/
// QFileInfo file_info("");
// QString name=file_info.fileName();
QIcon icon = icon_provider.icon(path);//获取该路径下文件图标;
if (count >= 1 && count < 9)//布局整体分为3列,每列8个,且已经计算好每个按钮所占大小和整个程序窗口大小相协调;
{
button = new buttonclicked(this);
button->setAutoRaise(true);//按钮离开时。按钮恢复到弹起状态
//button->setStyleSheet("QtoolButton{background-color:black;border:4px solid gray;border-radius:10px;}");
/*button->setAutoFillBackground(true);*/
/*button->setStyleSheet(0);*/
button->setIcon(icon);
button->setIconSize(QSize(32, 32));
button->setText(path);
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
button->setGeometry(QRect(30, (count-1)*70+10, 40, 40));//参数为x,y,按钮矩形长和宽;
button->show();
}
else if (count >= 9 && count < 17)
{
button = new buttonclicked(this);
button->setAutoRaise(true);//按钮离开时。按钮恢复到弹起状态
button->setIcon(icon);
button->setIconSize(QSize(32,32));
button->setText(path);
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
button->setGeometry(QRect(100, (count - 9) * 70 + 10, 40, 40));
button->show();
}
else if (count >= 17 && count <= 24)
{
button = new buttonclicked(this);
button->setAutoRaise(true);//按钮离开时。按钮恢复到弹起状态
button->setIcon(icon);
button->setIconSize(QSize(32,32));
button->setText(path);
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
button->setGeometry(QRect(170, (count - 17) * 70 + 10, 40, 40));
button->show();
}
else if (count > 24)
{
setAcceptDrops(false);//当拖进count>24时,不放进窗口;
}
}
return;
}
void atuohidewidget::close_buttton_clicked()
{
this->close();//Closes this widget
}
void atuohidewidget::input_path(QString path)//此处负责存储路径;
{
m_path.addInfor(path);
m_path.save();
}
void atuohidewidget::reload()
{
count = m_path.return_m_count();
QVector path = m_path.return_array();
for (int i = 0; i < count; i++)
{
QIcon icon = icon_provider.icon(path[i]);
if (i>= 0 && i < 8)
{
button = new buttonclicked(this);
button->setAutoRaise(true);//按钮离开时。按钮恢复到弹起状态
//button->setStyleSheet("QtoolButton{background-color:black;border:4px solid gray;border-radius:10px;}");
/*button->setAutoFillBackground(true);*/
button->setStyleSheet(0);
button->setIcon(icon);
button->setIconSize(QSize(32, 32));
button->setText(path[i]);
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
button->setGeometry(QRect(30, (i) * 70 + 10, 40, 40));
button->show();
}
else if (i >= 8 && i < 16)
{
button = new buttonclicked(this);
button->setAutoRaise(true);//按钮离开时。按钮恢复到弹起状态
button->setIcon(icon);
button->setIconSize(QSize(32, 32));
button->setText(path[i]);
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
button->setGeometry(QRect(100, (i-8) * 70 + 10, 40, 40));
button->show();
}
else if (i >= 16 && i <= 23)
{
button = new buttonclicked(this);
button->setIcon(icon);
button->setIconSize(QSize(32, 32));
button->setText(path[i]);
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
button->setGeometry(QRect(170, (count - 16) * 70 + 10, 40, 40));
button->show();
}
}
}
buttonclicked.h
#pragma once
#include
#include
#include
class buttonclicked : public QToolButton//重写qtoolbutton类;
{
Q_OBJECT
public:
buttonclicked(QWidget *parent);
void mouseDoubleClickEvent(QMouseEvent*event);
~buttonclicked();
private:
QWidget*parent;
};
buttonclicked.cpp
#include "buttonclicked.h"
buttonclicked::buttonclicked(QWidget *parent)
: QToolButton(parent)
{
parent = parent;
}
buttonclicked::~buttonclicked()
{
}
void buttonclicked::mouseDoubleClickEvent(QMouseEvent*event)
{
if (event->button() == Qt::LeftButton)
{
QString path = this->text();
ShellExecute(NULL, L"open", path.toStdWString().c_str(), NULL, NULL, SW_SHOWNORMAL);//获取该按钮内文本属性即路径,并且在桌面上根据路径打开文件;
}
}
path.h
#ifndef _PATH_H
#define _PATH_H
#include
#include
class path//对所选文件路径的运行进程时加载和存储以及添加进容器
{
public:
path();
~path();
int load();
int save();
void addInfor(QString & infor);
int return_m_count();
QVector & return_array();
private:
QVector m_choosed_path;//容器存储所选择的文件路径;
int m_count; //当前有几个
};
#endif
path.cpp
#include "path.h"
#include
path::path()
{
m_count = 0;
}
path::~path()
{
}
int path::load()
{
// 解析xml
TiXmlDocument xml_doc;
if (!xml_doc.LoadFile("path.xml"))
{
return -1;
}
// 根节点
TiXmlElement* xml_root = xml_doc.RootElement();
if (NULL == xml_root)
{
return -1;
}
TiXmlElement* xml_infor = xml_root->FirstChildElement("infor");
while (xml_infor)
{
m_choosed_path.push_back(QString(xml_infor->GetText()));
m_count++;
xml_infor = xml_infor->NextSiblingElement("infor");
}
return 0;
}
int path::save()
{
TiXmlDocument xml_doc;
xml_doc.LinkEndChild(new TiXmlDeclaration("1.0", "utf8", ""));
TiXmlElement * xml_root = new TiXmlElement("root");
xml_doc.LinkEndChild(xml_root);
for (auto i:m_choosed_path)
{
AfTinyXml::addChild(xml_root, "infor",i.toLocal8Bit().toStdString());
}
xml_doc.SaveFile("path.xml");//所有文件路径存储在xml文档里;
return 0;
}
void path::addInfor(QString & infor)
{
m_choosed_path.push_back(infor);
m_count++;//当前存储数量+1;
}
int path::return_m_count()
{
return m_count;
}
QVector & path::return_array()
{
return m_choosed_path;
}
还有就是xml语言存储路径数据的代码
给个链接自己下载tinyxml里代码加到项目里;点击打开链接
找到补充篇百度网盘xml 里tinyxml;
运行截图如图:
完成;