Qt实现360桌面精灵
2016-11-22 10:48
435 查看
有两个QLabel, 一个显示静态图片, 一个显示动态图片, 相互切换显示 ..
[cpp] view
plain copy
#ifndef TESTCHICKEN_H
#define TESTCHICKEN_H
#include <QtWidgets/QDialog>
#include "ui_testChicken.h"
#include <QPoint>
class QMovie;
class testChicken : public QDialog
{
Q_OBJECT
public:
testChicken(QWidget *parent = 0);
~testChicken();
private slots:
void OnTimerOut();
private:
virtual void mousePressEvent( QMouseEvent* e);
virtual void mouseMoveEvent( QMouseEvent* e);
virtual void mouseReleaseEvent( QMouseEvent * e);
bool CheckDragRegion(const QPoint &g_pos);
private:
Ui::testChickenClass ui;
QMovie *movie;
bool mbDragEnabled;
QPoint mRelativePos;
};
#endif // TESTCHICKEN_H
[cpp] view
plain copy
#include "testChicken.h"
#include <QMovie>
#include <QTimer>
#include <QMouseEvent>
testChicken::testChicken(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
mbDragEnabled = false;
mRelativePos = QPoint(0,0);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
QPixmap pixmap("AnZai.png");
ui.labImage->setPixmap(pixmap);
movie = new QMovie("AnZai.gif");
ui.labAnimation->setMovie(movie);
ui.labAnimation->hide();
QTimer* timer = new QTimer;
timer->setInterval(3000);
timer->start();
connect(timer, SIGNAL(timeout()), this, SLOT(OnTimerOut()));
}
testChicken::~testChicken()
{
}
void testChicken::OnTimerOut()
{
if(movie->state() == QMovie::Running) {
movie->stop();
ui.labAnimation->hide();
ui.labImage->show();
}
else {
movie->start();
ui.labAnimation->show();
ui.labImage->hide();
}
}
void testChicken::mousePressEvent( QMouseEvent* e )
{
if(CheckDragRegion(e->globalPos())) {
mbDragEnabled = true;
mRelativePos= pos() - e->globalPos();
}
}
void testChicken::mouseMoveEvent( QMouseEvent* e )
{
if(mbDragEnabled) {
move(e->globalPos()+ mRelativePos);
}
}
void testChicken::mouseReleaseEvent( QMouseEvent * e )
{
if(mbDragEnabled) {
mbDragEnabled = false;
}
}
bool testChicken::CheckDragRegion( const QPoint &g_pos )
{
QPoint pt = g_pos;
QRect rcWnd = geometry();
//qDebug() << "pos: " << pt << "geometry: " << rcWnd;
pt.setX(pt.x() - rcWnd.left());
pt.setY(pt.y() - rcWnd.top());
int nW = rcWnd.width();
int nH = rcWnd.height();
QRect rcDrag(0, 0, nW, nH);
if(rcDrag.contains(pt)) {
return true;
}
return false;
}
原文链接:http://blog.csdn.net/robertkun/article/details/27096677
[cpp] view
plain copy
#ifndef TESTCHICKEN_H
#define TESTCHICKEN_H
#include <QtWidgets/QDialog>
#include "ui_testChicken.h"
#include <QPoint>
class QMovie;
class testChicken : public QDialog
{
Q_OBJECT
public:
testChicken(QWidget *parent = 0);
~testChicken();
private slots:
void OnTimerOut();
private:
virtual void mousePressEvent( QMouseEvent* e);
virtual void mouseMoveEvent( QMouseEvent* e);
virtual void mouseReleaseEvent( QMouseEvent * e);
bool CheckDragRegion(const QPoint &g_pos);
private:
Ui::testChickenClass ui;
QMovie *movie;
bool mbDragEnabled;
QPoint mRelativePos;
};
#endif // TESTCHICKEN_H
[cpp] view
plain copy
#include "testChicken.h"
#include <QMovie>
#include <QTimer>
#include <QMouseEvent>
testChicken::testChicken(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
mbDragEnabled = false;
mRelativePos = QPoint(0,0);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
QPixmap pixmap("AnZai.png");
ui.labImage->setPixmap(pixmap);
movie = new QMovie("AnZai.gif");
ui.labAnimation->setMovie(movie);
ui.labAnimation->hide();
QTimer* timer = new QTimer;
timer->setInterval(3000);
timer->start();
connect(timer, SIGNAL(timeout()), this, SLOT(OnTimerOut()));
}
testChicken::~testChicken()
{
}
void testChicken::OnTimerOut()
{
if(movie->state() == QMovie::Running) {
movie->stop();
ui.labAnimation->hide();
ui.labImage->show();
}
else {
movie->start();
ui.labAnimation->show();
ui.labImage->hide();
}
}
void testChicken::mousePressEvent( QMouseEvent* e )
{
if(CheckDragRegion(e->globalPos())) {
mbDragEnabled = true;
mRelativePos= pos() - e->globalPos();
}
}
void testChicken::mouseMoveEvent( QMouseEvent* e )
{
if(mbDragEnabled) {
move(e->globalPos()+ mRelativePos);
}
}
void testChicken::mouseReleaseEvent( QMouseEvent * e )
{
if(mbDragEnabled) {
mbDragEnabled = false;
}
}
bool testChicken::CheckDragRegion( const QPoint &g_pos )
{
QPoint pt = g_pos;
QRect rcWnd = geometry();
//qDebug() << "pos: " << pt << "geometry: " << rcWnd;
pt.setX(pt.x() - rcWnd.left());
pt.setY(pt.y() - rcWnd.top());
int nW = rcWnd.width();
int nH = rcWnd.height();
QRect rcDrag(0, 0, nW, nH);
if(rcDrag.contains(pt)) {
return true;
}
return false;
}
原文链接:http://blog.csdn.net/robertkun/article/details/27096677
相关文章推荐
- QT之桌面精灵实现(一) --- 透明窗体和图片绘制
- QT之桌面精灵实现(二) --- 右键菜单、开机启动、窗口Z-Order
- Qt 实现桌面雪花飘落 - 修改1
- 用Qt实现一个桌面弹幕程序(六)-- -- 桌面客户端实现②
- Qt 实现桌面雪花飘落 - 修改3(终结)
- 浅析桌面精灵的实现
- Qt实现嵌入桌面的半透明窗口
- 面向对象编程实践—桌面时钟的设计(Qt实现)
- Qt 实现桌面雪花飘落 - 修改2
- Qt日记(2)-360新版特性界面实现(2)
- Qt实现桌面截屏
- Qt实现嵌入桌面的半透明窗口
- Qt实现桌面悬浮球/不规则窗体
- DELPHI实现类似仿360桌面的程序界面
- 用Qt实现一个桌面弹幕程序(四)--实现一个弹幕③
- Qt日记(1)-360新版特性界面实现(1)
- 用Qt实现一个桌面弹幕程序(五)-- -- 桌面客户端实现①
- 模仿实现360桌面水晶球式的一键清理特效
- 用Qt实现一个桌面弹幕程序(九)-- -- 服务端的实现
- Qt移动应用开发(三):使用精灵图片实现帧动画