900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > QT自定义按钮类(自定义图片 形状 点击特效)

QT自定义按钮类(自定义图片 形状 点击特效)

时间:2022-06-25 08:21:00

相关推荐

QT自定义按钮类(自定义图片 形状 点击特效)

在QTQTQT中如何实现这种按钮??

要求能够自定义图片,不规则大小,且点击后会有上下抖动状态,比如下面这个球

那我们就封装自己的一个按钮类,就叫MyPushButtonMyPushButtonMyPushButton

继承于QWidgetQWidgetQWidget,然后在MyPushButton.hMyPushButton.hMyPushButton.h中改一下,继承于QPushButtonQPushButtonQPushButton

class MyPushButton : public QPushButton

在MyPushButton.cppMyPushButton.cppMyPushButton.cpp也改一下,继承于QPushButtonQPushButtonQPushButton

先看MyPushButton.hMyPushButton.hMyPushButton.h

class MyPushButton : public QPushButton{Q_OBJECTpublic:// explicit MyPushButton(QWidget *parent = nullptr);//构造函数,参数:正常的图片路径,按下后的图片路径MyPushButton(QString normal,QString press="");QString normal_img;//正常路径QString press_img;//按下后的路径void move1(int val);//移动函数

然后来实现移动函数,让yyy坐标移动valvalval

这个简单,用−>pos(){->}pos()−>pos()方法获取QPointQPointQPoint,然后修改yyy再设置回去就好了

void MyPushButton::move1(int val){QPoint w = this->pos();w.setY(w.y()+val);this->move(w);qDebug() << "啥玩意" << val ;}

然后来实现构造函数,第一个参数的默认的图片路径,第二个参数是点击后的图片路径

MyPushButton::MyPushButton(QString nor,QString pre){this->normal_img = nor;//暂时储存一下图片路径this->press_img = pre;QPixmap map;bool YES = map.load(nor);//加载路径if( !YES ){qDebug() << "图片加载失败";return;}//设置图片大小this->setFixedSize(map.width(),map.height());//设置按钮的固定大小为图片大小//设置不规则图片样式this->setStyleSheet("QPushButton{border:0px;}");//设置按钮为图片的形状//设置图片this->setIcon(map);//把图片放进去//设置图标大小this->setIconSize(QSize(map.width(),map.height()));//设置图片在按钮上的显示大小}

一开始我奇怪setFixedSizesetFixedSizesetFixedSize和setIconSizesetIconSizesetIconSize怎么都是设置大小的

原来前一个是设置按钮的大小,后一个是设置图片在按钮上的显示大小,是不一样的

然后使用这个按钮类就很简单了,如下所示

MyPushButton *start = new MyPushButton(QString(":/res/MenuSceneStartButton.png"));start->setParent(this);start->move( this->width()*0.5-start->width()*0.5, this->height()-start->height()*1.2);//设置开始按钮点击特效connect(start,&MyPushButton::pressed,[=](){start->move1(-10);});connect(start,&MyPushButton::released,[=](){start->move1(10);

效果就是开始的那张图。

MyPushButton.hMyPushButton.hMyPushButton.h

#ifndef MYPUSHBUTTON_H#define MYPUSHBUTTON_H#include <QWidget>#include <QPushButton>class MyPushButton : public QPushButton{Q_OBJECTpublic:// explicit MyPushButton(QWidget *parent = nullptr);//构造函数,参数:正常的图片路径,按下后的图片路径MyPushButton(QString normal,QString press="");QString normal_img;//正常路径QString press_img;//按下后的路径void move1(int val);//移动函数signals:};#endif // MYPUSHBUTTON_H

MyPushButton.cppMyPushButton.cppMyPushButton.cpp

#include "mypushbutton.h"#include <QDebug>#include <QPoint>//MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)//{//}void MyPushButton::move1(int val){QPoint w = this->pos();w.setY(w.y()+val);this->move(w);qDebug() << "啥玩意" << val ;}MyPushButton::MyPushButton(QString nor,QString pre){this->normal_img = nor;this->press_img = pre;QPixmap map;bool YES = map.load(nor);if( !YES ){qDebug() << "图片加载失败";return;}//设置图片大小this->setFixedSize(map.width(),map.height());//设置不规则图片样式this->setStyleSheet("QPushButton{border:0px;}");//设置图片this->setIcon(map);//设置图标大小// this->setIconSize(QSize(map.width(),map.height()));}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。