欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT自绘封装AnimationButton类

程序员文章站 2022-05-28 14:33:40
...

一、本类参考于feiyangqingyun作品改编,80%的成果属于原作者。原作品下载地址:https://download.csdn.net/download/zhangxiaoyu_sy/10178816

主要更改:

1、鼠标点击图片放大显示,鼠标再次点击图片缩小显示;

2、增加一组按钮的排它性设置,同一时刻只能有一个按钮按下;

1、类的封装

#ifndef ANIMATIONBUTTON_H
#define ANIMATIONBUTTON_H

#include <QWidget>
#include <QVariant>
#include <QPushButton>

class QPropertyAnimation;

class AnimationButton : public QPushButton
{
    Q_OBJECT
public:
    explicit AnimationButton(QWidget *parent = 0);
    ~AnimationButton();

protected:
    void paintEvent(QPaintEvent *);
	void mousePressEvent(QMouseEvent* event);

private:
	bool bPressed;
	int minPixWidth;                         //图片缩小状态宽度
	int minPixHeight;                        //图片缩小状态高度
	int oldWidth;                            //图片原始宽度
	int oldHeight;                           //图片原始高度
	int pixWidthtemp;                        //图片临时宽度
	int pixHeighttemp;                       //图片临时高度
	int targetWidth;                         //绘制目标宽度
	int targetHeight;                        //绘制目标高度

    QPropertyAnimation *enterAnimation; //点击动画
    QPropertyAnimation *leaveAnimation; //释放动画

    QString text;                       //显示文字
    QString image;                      //图像路径

private slots:
    void enterImageChanged(QVariant index);
    void leaveImageChanged(QVariant index);

public slots:
    //设置显示的文字
    void setText(QString text);
    //设置显示的图像
    void setImage(QString image);

	void setPressed(bool bpress);
	bool getPressed();

signals:
	void btnPressed(bool bpress);
};

#endif // ANIMATIONBUTTON_H
#include "animationbutton.h"
#include <QPropertyAnimation>
#include <QPainter>
#include <QDebug>
#include <QMouseEvent>

AnimationButton::AnimationButton(QWidget *parent) : QPushButton(parent)
{
	pixWidthtemp = 0;
	pixHeighttemp = 0;

	oldWidth = 0;
	oldHeight = 0;

	minPixWidth = 0;
	minPixHeight = 0;

	bPressed = false;

	/************************************************************************/
	/*  QPropertyAnimation类定义了QT的属性动画
	/*  参数1 this表示动画作用的QObject对象;参数2表示QObject的属性
	/************************************************************************/
	enterAnimation = new QPropertyAnimation(this, "Max");
	enterAnimation->setStartValue(0);
	enterAnimation->setEndValue(5);
	enterAnimation->setDuration(200);
	connect(enterAnimation, SIGNAL(valueChanged(QVariant)), this, SLOT(enterImageChanged(QVariant)));

	leaveAnimation = new QPropertyAnimation(this, "Min");
	leaveAnimation->setStartValue(0);
	leaveAnimation->setEndValue(5);
	leaveAnimation->setDuration(200);
	connect(leaveAnimation, SIGNAL(valueChanged(QVariant)), this, SLOT(leaveImageChanged(QVariant)));
}

AnimationButton::~AnimationButton()
{
	delete enterAnimation;
	delete leaveAnimation;
}

void AnimationButton::paintEvent(QPaintEvent *)
{
	if (image.isEmpty()) {
		return;
	}

	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);

	QPixmap pix(image);
    pix = pix.scaled(targetWidth, targetHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

	if (true) {
		int pixX = this->rect().center().x() - targetWidth / 2;
		int pixY = this->rect().center().y() - targetHeight / 2 - 10;
		QPoint point(pixX, pixY);
		painter.drawPixmap(point, pix);
		painter.drawText(QRectF(0, height() - 20, width(), 20), Qt::AlignCenter, text);
	}
}

void AnimationButton::mousePressEvent(QMouseEvent* event)
{
	if (event->type() == QEvent::MouseButtonPress)
	{
		if (event->button() == Qt::LeftButton)
		{
			bPressed = !bPressed;

			if (bPressed)
			{
				pixWidthtemp = minPixWidth;
				pixHeighttemp = minPixHeight;

				emit btnPressed(true);

				enterAnimation->start();
			} 
			else
			{
				pixWidthtemp = oldWidth;
				pixHeighttemp = oldHeight;

				leaveAnimation->start();
			}
		}
	}
}

void AnimationButton::enterImageChanged(QVariant index)
{
	int i = index.toInt();
	targetWidth = pixWidthtemp + i * 5;
	targetHeight = pixHeighttemp + i * 5;
	update();
}

void AnimationButton::leaveImageChanged(QVariant index)
{
	int i = index.toInt();
	targetWidth = pixWidthtemp - i * 5;
	targetHeight = pixWidthtemp - i * 5;
	update();
}

void AnimationButton::setImage(QString image)
{
	this->image = image;
	QPixmap pix(image);

	pixWidthtemp = pix.width();
	pixHeighttemp = pix.height();

	oldWidth = pixWidthtemp;
	oldHeight = pixHeighttemp;

	minPixWidth = pixWidthtemp - 25;
	minPixHeight = pixHeighttemp - 25;
	
	targetWidth = minPixWidth;
	targetHeight = minPixHeight;

	update();
}

void AnimationButton::setText(QString text)
{
	this->text = text;
	update();
}

void AnimationButton::setPressed(bool bpress)
{
	bPressed = bpress;

	pixWidthtemp = oldWidth;
	pixHeighttemp = oldHeight;

	leaveAnimation->start();

	update();
}

bool AnimationButton::getPressed()
{
	return bPressed;
}

2、类的使用

#ifndef ANIMATIONBTN_H
#define ANIMATIONBTN_H

#include <QtGui/QWidget>
#include "ui_animationbtn.h"
#include "animationbutton.h"

class AnimationBtn : public QWidget
{
	Q_OBJECT

public:
	AnimationBtn(QWidget *parent = 0, Qt::WFlags flags = 0);
	~AnimationBtn();

private:
	Ui::AnimationBtnClass ui;

	QVector <AnimationButton* > m_vecBtn;

private slots:
	void OnBtnPressed(bool bpress);
};

#endif // ANIMATIONBTN_H
#include "animationbtn.h"

AnimationBtn::AnimationBtn(QWidget *parent, Qt::WFlags flags)
	: QWidget(parent, flags)
{
	ui.setupUi(this);
	ui.btn1->setText(QString::fromLocal8Bit("主页"));
	ui.btn1->setImage(":/AnimationBtn/Resources/v-home-ico-contact.png");

	ui.btn2->setText(QString::fromLocal8Bit("通讯录"));
	ui.btn2->setImage(":/AnimationBtn/Resources/v-home-ico-home.png");

	ui.btn3->setText(QString::fromLocal8Bit("图片"));
	ui.btn3->setImage(":/AnimationBtn/Resources/v-home-ico-img.png");

	ui.btn4->setText(QString::fromLocal8Bit("录音"));
	ui.btn4->setImage(":/AnimationBtn/Resources/v-home-ico-record.png");

	m_vecBtn.push_back(ui.btn1);
	m_vecBtn.push_back(ui.btn2);
	m_vecBtn.push_back(ui.btn3);
	m_vecBtn.push_back(ui.btn4);
	
	for (int i = 0; i != m_vecBtn.size(); ++i)
	{
		connect(m_vecBtn[i],SIGNAL(btnPressed(bool)),this,SLOT(OnBtnPressed(bool)));
	}
}

AnimationBtn::~AnimationBtn()
{

}

/************************************************************************/
/* 设置按钮之间的排它性:一个按钮按下,其余按钮要弹起                      */
/************************************************************************/
void AnimationBtn::OnBtnPressed(bool bpress)
{
	AnimationButton* pBtn = (AnimationButton*)sender();

	for (int i = 0; i != m_vecBtn.size(); ++i)
	{
		if (pBtn != m_vecBtn[i] && m_vecBtn[i]->getPressed())
		{
			m_vecBtn[i]->setPressed(false);
		}
	}
}
#include "animationbtn.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	AnimationBtn w;
	w.show();
	return a.exec();
}

QT自绘封装AnimationButton类

 

再次感谢:feiyangqingyun