#pragma once
#include<QPushButton>
#include<QPainter>
#include<QMouseEvent>
class push_button : public QPushButton
{
Q_OBJECT
public:
push_button(QWidget *parent=0);
~push_button();
void setPicName(QString pic_name); //图片路径
protected:
void enterEvent(QEvent *);
void leaveEvent(QEvent *);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *);
private:
//枚举按钮的几种状态
enum ButtonStatus{NORMAL,ENTER,PRESS,NOSTATUS};
ButtonStatus status;
QString pic_name;
int btn_width; //按钮宽度
int btn_height; //按钮高度
int mouse_press;//按钮左键是否按下
};
#include "push_button.h"
push_button::push_button(QWidget *parent)
: QPushButton(parent)
{
//初始化状态
status = NORMAL;
mouse_press = false;
}
push_button::~push_button()
{
}
void push_button::setPicName(QString pic_name)
{
this->pic_name = pic_name;
setFixedSize(QPixmap(pic_name).size());//获取图片大小
}
void push_button::enterEvent(QEvent *)
{
status = ENTER;
//当窗口程序需要升级或者重新绘制时,调用此成员函数.使用 repaint()和 update() 后,调用函数 paintEvent()。
update();
}
void push_button::leaveEvent(QEvent *)
{
status = NORMAL;
update();
}
void push_button::mousePressEvent(QMouseEvent *event)
{
//点击鼠标左键
if (event->button()==Qt::LeftButton)
{
mouse_press = true;
status = PRESS;
update();
}
}
void push_button::mouseReleaseEvent(QMouseEvent *event)
{
//若已经点击,则将它处理为仅在这个图标上放的,也就是enter状态
if (mouse_press&&this->rect().contains(event->pos()))
{
mouse_press = false;
status = ENTER;
update();
emit clicked();
}
}
void push_button::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pixmap;
switch (status)
{
case NORMAL:
pixmap.load(pic_name); //加载图片
break;
case ENTER:
pixmap.load(pic_name+QString("_hover"));
break;
case PRESS:
pixmap.load(pic_name+QString("_pressed"));
break;
case NOSTATUS:
pixmap.load(pic_name);
break;
default:
pixmap.load(pic_name);
}
painter.drawPixmap(rect(), pixmap);
//这里的rect()是指当前窗体的显示区域
}
#include "tool_button.h"
tool_button::tool_button(QString pic_name,QWidget *parent)
: QToolButton(parent)
{
//初始化
//设置文本颜色 一会总结用法(如电脑体验)
QPalette text_palette = palette();
text_palette.setColor(QPalette::ButtonText,QColor(30,30,30));
setPalette(text_palette);
//设置文本粗体
QFont &text_font = const_cast<QFont &>(font());
text_font.setWeight(QFont::Bold);
//设置文字显示在图片的下面
setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
//设置图标
QPixmap pixmap(pic_name);
setIcon(pixmap);
setIconSize(pixmap.size());
//设置大小
setFixedSize(pixmap.width()+25,pixmap.height()+25);
setAutoRaise(true); //图标自动浮起
setObjectName("transparentToolButton"); //设置对象名
mouse_over = false;
mouse_press = false;
}
tool_button::~tool_button()
{
}
void tool_button::enterEvent(QEvent *)
{
mouse_over = true;
}
void tool_button::leaveEvent(QEvent *)
{
mouse_over = false;
}
void tool_button::mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::LeftButton)
{
emit clicked();
}
}
void tool_button::setMousePress(bool mouse_press)
{
this->mouse_press = mouse_press;
update();
}
void tool_button::paintEvent(QPaintEvent *event)
{
if (mouse_over)
{
//绘制鼠标移到按钮上的按钮效果
painterInfo(0,100,150);
}
else
{
if (mouse_press)
{
painterInfo(150, 100, 0);
}
}
QToolButton::paintEvent(event);
}
//点击时,出现一个矩形框
void tool_button::painterInfo(int top_color, int middle_color, int botton_color)
{
QPainter painter(this);
QPen pen(Qt::NoBrush,1);
painter.setPen(pen);
//指定一个线性渐变画笔
QLinearGradient linear(rect().topLeft(),rect().bottomLeft());
linear.setColorAt(0,QColor(230,230,230,top_color));
linear.setColorAt(0.5, QColor(210, 210, 210,middle_color));
linear.setColorAt(1, QColor(190, 190, 190, botton_color));
painter.setBrush(linear);
painter.drawRect(rect());
}
#include "tool_button.h"
tool_button::tool_button(QString pic_name,QWidget *parent)
: QToolButton(parent)
{
//初始化
//设置文本颜色 一会总结用法(如电脑体验)
QPalette text_palette = palette();
text_palette.setColor(QPalette::ButtonText,QColor(30,30,30));
setPalette(text_palette);
//设置文本粗体
QFont &text_font = const_cast<QFont &>(font());
text_font.setWeight(QFont::Bold);
//设置文字显示在图片的下面
setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
//设置图标
QPixmap pixmap(pic_name);
setIcon(pixmap);
setIconSize(pixmap.size());
//设置大小
setFixedSize(pixmap.width()+25,pixmap.height()+25);
setAutoRaise(true); //图标自动浮起
setObjectName("transparentToolButton"); //设置对象名
mouse_over = false;
mouse_press = false;
}
tool_button::~tool_button()
{
}
void tool_button::enterEvent(QEvent *)
{
mouse_over = true;
}
void tool_button::leaveEvent(QEvent *)
{
mouse_over = false;
}
void tool_button::mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::LeftButton)
{
emit clicked();
}
}
void tool_button::setMousePress(bool mouse_press)
{
this->mouse_press = mouse_press;
update();
}
void tool_button::paintEvent(QPaintEvent *event)
{
if (mouse_over)
{
//绘制鼠标移到按钮上的按钮效果
painterInfo(0,100,150);
}
else
{
if (mouse_press)
{
painterInfo(150, 100, 0);
}
}
QToolButton::paintEvent(event);
}
//点击时,出现一个矩形框
void tool_button::painterInfo(int top_color, int middle_color, int botton_color)
{
QPainter painter(this);
QPen pen(Qt::NoBrush,1);
painter.setPen(pen);
//指定一个线性渐变画笔
QLinearGradient linear(rect().topLeft(),rect().bottomLeft());
linear.setColorAt(0,QColor(230,230,230,top_color));
linear.setColorAt(0.5, QColor(210, 210, 210,middle_color));
linear.setColorAt(1, QColor(190, 190, 190, botton_color));
painter.setBrush(linear);
painter.drawRect(rect());
}