QToolButton按钮背景图片修改

实现步骤:

1、    设置setStyleSheet的样式

Btn->setStyleSheet("QToolButton{border:Opx}");

2、    设置按钮的尺寸自适应图片大小

Btn->setIconSize(QSize(Btn->width(),Btn->height()));

3、    新建QPixmap类,并加载按钮的图片背景图片

QPixmap pixmap(path_image);

Btn->setIcon(QIcon(pixmap));

程序:

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "QImage"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QImage image;
    image.load(":/image/6631506470840007614.jpg");
    QPalette palette;
    palette.setBrush(this->backgroundRole(),QBrush(image));
    this->setPalette(palette);
    setBtnIcon(ui->open, ":/image/open.png");
    setBtnIcon(ui->large, ":/image/big.png");
    setBtnIcon(ui->litte, ":/image/litte.png");
    setBtnIcon(ui->next, ":/image/next.png");
    setBtnIcon(ui->previous, ":/image/previous.png");
    setBtnIcon(ui->normal, ":/image/normal.png");
    setBtnIcon(ui->recovery, ":/image/recover.png");
    setBtnIcon(ui->start, ":/image/start.png");
    setBtnIcon(ui->stop, ":/image/stop.png");
    setBtnIcon(ui->rotate, ":/image/zhuan.png");
    setBtnIcon(ui->open, ":/image/open.png");

}

Widget::~Widget()
{
    delete ui;
}
void Widget::setBtnIcon(QToolButton *Btn, const QString &path_image)
{
    Btn->setStyleSheet("QToolButton{border:Opx}");
    Btn->setIconSize(QSize(Btn->width(),Btn->height()));
    QPixmap pixmap(path_image);
    Btn->setIcon(QIcon(pixmap));
}

2、widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include
#include "QToolButton"
#include "QLabel"
#include "QTimer"
#include "QString"
#include "QPixmap"
#include "QPalette"
#include "QMatrix"
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private:
    void setBtnIcon(QToolButton *Btn, const QString &path_image);
private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
3、运行结果

QToolButton按钮背景图片修改_第1张图片

你可能感兴趣的:(QT,C++)