QTanimationbutton

#ifndef ANIMATIONBUTTON_H

#define ANIMATIONBUTTON_H

#include <QWidget>

#include <QVariant>

class QPropertyAnimation;

class AnimationButton : public QWidget

{

Q_OBJECT

public:

explicit AnimationButton(QWidget *parent = nullptr);

~AnimationButton();

protected:

void enterEvent(QEvent *event);

void leaveEvent(QEvent *event);

void paintEvent(QPaintEvent *event);

private:

bool enter;

bool leave;

int pixwidth;

int pixheight;

int oldwidth;

int oldheight;

int targetwidth;

int targetheight;

QString text;

QString image;

QPropertyAnimation *enteranimation;

QPropertyAnimation *leaveanimation;

private slots:

void enterimagechanged(QVariant index);

void leaveimagechanged(QVariant index);

public slots:

void settext(QString text);

void setimage(QString image);

signals:

};

#endif // ANIMATIONBUTTON_H


#include "animationbutton.h"

#include "qpainter.h"

#include "qdebug.h"

#include "qpropertyanimation.h"

AnimationButton::AnimationButton(QWidget *parent) : QWidget(parent)

{

enter=true;

leave=false;

pixwidth=0;

pixheight=0;

oldwidth=0;

oldheight=0;

enteranimation = new QPropertyAnimation(this,"");

enteranimation->setStartValue(0);

enteranimation->setEndValue(5);

enteranimation->setDuration(200);

connect(enteranimation,&QPropertyAnimation::valueChanged,this,&AnimationButton::enterimagechanged);

leaveanimation = new QPropertyAnimation(this,"");

leaveanimation->setStartValue(0);

leaveanimation->setEndValue(5);

leaveanimation->setDuration(200);

connect(leaveanimation,&QPropertyAnimation::valueChanged,this,&AnimationButton::leaveimagechanged);

}

AnimationButton::~AnimationButton()

{

delete enteranimation;

delete leaveanimation;

}

void AnimationButton::enterEvent(QEvent *event)

{

enter=true;

leave=false;

pixwidth=pixwidth-25;

pixheight=pixheight-25;

enteranimation->start();

}

void AnimationButton::leaveEvent(QEvent *event)

{

enter=false;

leave=true;

pixwidth=oldwidth;

pixheight=oldheight;

leaveanimation->start();

}

void AnimationButton::paintEvent(QPaintEvent *event)

{

if (image.isEmpty())

{

return;

}

QPainter painter(this);

painter.setRenderHint(QPainter::Antialiasing);

QPixmap pix(image);

pix=pix.scaled(targetwidth,targetheight,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);

if(enter || leave)

{

int pixX=rect().center().x()-targetwidth/2;

int pixY=rect().center().y()-targetheight/2;

QPoint point(pixX,pixY);

painter.drawPixmap(point,pix);

painter.drawText(QRectF(0,height()-20,width(),20),Qt::AlignCenter,text);

}

}

void AnimationButton::enterimagechanged(QVariant index)

{

int i=index.toInt();

targetwidth=pixwidth+i*5;

targetheight=pixheight+i*5;

update();

}

void AnimationButton::leaveimagechanged(QVariant index)

{

int i=index.toInt();

targetwidth=pixwidth-i*5;

targetheight=pixheight-i*5;

update();

}

void AnimationButton::settext(QString text)

{

this->text=text;

update();

}

void AnimationButton::setimage(QString image)

{

this->image=image;

QPixmap pix(image);

pixwidth=pix.width();

pixheight=pix.height();

oldwidth=pixwidth;

oldheight=pixheight;

targetwidth=pixwidth-25;

targetheight=pixheight-25;

update();

}


#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE

namespace Ui { class Widget; }

QT_END_NAMESPACE

class Widget : public QWidget

{

Q_OBJECT

public:

Widget(QWidget *parent = nullptr);

~Widget();

private:

Ui::Widget *ui;

};

#endif // WIDGET_H


#include "widget.h"

#include "ui_widget.h"

Widget::Widget(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Widget)

{

ui->setupUi(this);

// QString qss="color:#BECEC6;background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #FFFFFF,stop:1 #EAF2FF)";

// QString qss="color:#BECEC6;background:qlineargradient(spread:pad,x1:0,y1:0,x2:1,y2:0,stop:0 #030303,stop:1 #2E8B57)";

// QString qss="color:black;background:qlineargradient(spread:pad,x1:0,y1:0,x2:1,y2:0,stop:0 green,stop:1 blue)";

QString qss="color:rgba(0,0,0,1);background:qlineargradient(spread:pad,x1:0,y1:0,x2:1,y2:0,stop:0 rgba(0, 255, 0, 255),stop:1 rgba(255, 255, 0, 255))";

this->setStyleSheet(qss);

ui->widget1->settext("inter");

ui->widget1->setimage(":/new/prefix1/image/inter.png");

ui->widget2->settext("other");

ui->widget2->setimage(":/new/prefix1/image/other.png");

ui->widget3->settext("user");

ui->widget3->setimage(":/new/prefix1/image/user.png");

ui->widget4->settext("set");

ui->widget4->setimage(":/new/prefix1/image/set.png");

ui->widget4->update();

}

Widget::~Widget()

{

delete ui;

}

相关推荐
颜颜yan_2 小时前
Git Cola 鸿蒙 PC 版适配全记录:从 Qt 桌面工具到 ArkTS 原生应用
git·qt·harmonyos
Lsir10110_2 小时前
从按钮“点不动“讲起——深入理解 Qt 信号槽机制
开发语言·qt·信号处理
lqj_本人3 小时前
WinMerge 鸿蒙 PC 适配全记录:以 Qt 重建桌面外壳,打通文件与文件夹差异闭环
qt·华为·harmonyos
lqj_本人3 小时前
Tftpd64 鸿蒙 PC 适配全记录:用 Qt 重建一组可运行的 UDP 网络服务
qt·udp·harmonyos
User_芊芊君子3 小时前
RStudio 鸿蒙 PC 适配全记录:以 Qt 原生工作区承载嵌入式 R
qt·r语言·harmonyos
颜颜yan_5 小时前
Firebird 鸿蒙 PC 适配全记录:打通数据库内核、Qt 管理端与原生维护工具
数据库·qt·harmonyos
鸽芷咕6 小时前
Mu Editor 鸿蒙 PC 适配全记录:用 Qt 与嵌入式 CPython 重建教学型 Python IDE
python·qt·harmonyos
长友cy6 小时前
官方示例scene graph - vulkan阅读
qt
朽棘不雕6 小时前
Qt背景(简单了解Qt)
qt
颜颜yan_6 小时前
Geany 鸿蒙 PC 适配全记录:以 Qt 重建轻量 IDE,打通编辑、项目检索与命令执行
ide·qt·harmonyos