Qt 图片按钮的制作和使用
1.新建一个C++类
命名为buttons
buttons.h内容为:
1 #ifndef BUTTONS_H 2 #define BUTTONS_H 3 #include <QWidget> 4 #include <QPushButton> 5 #include <QPainter> 6 #include <QPaintEvent> 7 #include <QMouseEvent> 8 class buttons : public QPushButton 9 { 10 Q_OBJECT 11 public: 12 explicit buttons(QWidget *parent = nullptr); 13 //设置状态图片 14 void setImagePath(const QString &normal = QString("../../../image/max_normal.png"), 15 const QString &hover = QString("../../../image/max_hover.png"), 16 const QString &press = QString("../../../image/max_down.png")); 17 bool getIsHover() const; 18 void setIsHover(bool value); 19 bool getIsClicked() const; 20 void setIsClicked(bool value); 21 //设置按钮大小 22 QSize getSize() const; 23 void setSize(const QSize &size); 24 void setSize(int iw, int ih); 25 //根据鼠标状态,更新背景图片 26 void setPath(); 27 signals: 28 void sign_clicked(); 29 public slots: 30 private: 31 bool isClicked = false; 32 bool isHover = false; 33 QString path; 34 //正常状态图片路径 35 QString normalPath = "../../../image/max_normal.png"; 36 //鼠标在按钮上时的图片路径 37 QString hoverPath = "../../../image/max_hover.png"; 38 //鼠标按下时的图片路径 39 QString pressPath = "../../../image/max_down.png"; 40 QString tmpPath; 41 //按钮大小 42 int w = 32; 43 int h = 32; 44 QSize _size = QSize(w, h); 45 protected: 46 //鼠标点击事件 47 void mousePressEvent(QMouseEvent *); 48 //鼠标进入按钮边界和离开按钮边界事件 49 bool event(QEvent *e); 50 //重绘背景图片 51 void paintEvent(QPaintEvent *); 52 }; 53 #endif // BUTTONS_H
buttons.cpp内容为:
#include "buttons.h" buttons::buttons(QWidget *parent) : QPushButton(parent) { connect(this, &buttons::sign_clicked, this, &buttons::click); } void buttons::setImagePath(const QString &normal, const QString &hover, const QString &press) { normalPath = normal; hoverPath = hover; pressPath = press; setPath(); } bool buttons::getIsHover() const { return isHover; } void buttons::setIsHover(bool value) { isHover = value; } bool buttons::getIsClicked() const { return isClicked; } void buttons::setIsClicked(bool value) { isClicked = value; } QSize buttons::getSize() const { return _size; } void buttons::setSize(const QSize &size) { _size = size; w = getSize().width(); h = getSize().height(); this->setFixedSize(getSize()); update(); } void buttons::setSize(int iw, int ih) { if(iw <= 0 || ih <= 0) { return; } setSize(QSize(iw, ih)); update(); } void buttons::setPath() { tmpPath = getIsHover() ? hoverPath : normalPath; path = getIsClicked() ? pressPath : tmpPath; update(); } void buttons::mousePressEvent(QMouseEvent *) { emit sign_clicked(); setIsClicked(!getIsClicked()); setPath(); } bool buttons::event(QEvent *e) { if(e->type() == QEvent::Enter) { setIsHover(true); setPath(); } if(e->type() == QEvent::Leave) { setIsHover(false); setPath(); } return QWidget::event(e); } void buttons::paintEvent(QPaintEvent *) { resize(getSize()); if(path.isEmpty()) { return; } QPainter p; QImage img(path); img = img.scaled(getSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); p.begin(this); p.drawImage(this->rect(), img); p.end(); }
然后在主窗体中定义三个按钮,来实现最大化,最小化和关闭按钮
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include "buttons.h" namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); bool getIsMaxState() const; void setIsMaxState(bool value); private: Ui::Widget *ui; buttons *myBt = nullptr; buttons *myBt1 = nullptr; buttons *myBt2 = nullptr; bool isMaxState = false; protected: void resizeEvent(QResizeEvent *); }; #endif // WIDGET_H
Widget.cpp内容为:
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); myBt = new buttons(this); myBt->setSize(40, 40); myBt->move(width() - 3 * myBt->getSize().width(), 0); myBt->setImagePath("E:/image/sysbtn_min_normal.png", "E:/image/sysbtn_min_hover.png", "E:/image/sysbtn_min_down.png"); myBt1 = new buttons(this); myBt1->setSize(40, 40); myBt1->move(width() - 2 * myBt->getSize().width(), 0); myBt1->setImagePath(); myBt2 = new buttons(this); myBt2->setSize(40, 40); myBt2->move(width() - myBt->getSize().width(), 0); myBt2->setImagePath("E:/image/sysbtn_close_normal.png", "E:/image/sysbtn_close_hover.png", "E:/image/sysbtn_close_down.png"); connect(myBt, &buttons::clicked, this, [ = ]() { this->setWindowState(Qt::WindowMinimized); }); connect(myBt1, &buttons::clicked, this, [ = ]() { if(getIsMaxState()) { setIsMaxState(false); myBt1->setImagePath(); this->setWindowState(Qt::WindowNoState); } else { setIsMaxState(true); myBt1->setImagePath("E:/image/sysbtn_restore_normal.png", "E:/image/sysbtn_restore_hover.png", "E:/image/sysbtn_restore_down.png"); this->setWindowState(Qt::WindowMaximized); } }); connect(myBt2, &buttons::clicked, this, [ = ]() { this->close(); }); } Widget::~Widget() { delete ui; } bool Widget::getIsMaxState() const { return isMaxState; } void Widget::setIsMaxState(bool value) { isMaxState = value; } void Widget::resizeEvent(QResizeEvent *) { myBt->move(width() - 3 * myBt->getSize().width(), 0); myBt1->move(width() - 2 * myBt->getSize().width(), 0); myBt2->move(width() - myBt->getSize().width(), 0); }
实现效果如下: