Qt 圖片按鈕的制作和使用,實現最大化,最小化和關閉按鈕


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);
}

 

 
        
實現效果如下:

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM