头文件:
#include <qpainter.h>
#define BTN_STATUX_OK_PNG ":/Resources/MainWindow/StatusSucceeded.png"
函数定义:
void drawButtonIcon(QPushButton*&btn, const QString&icon);
源代码:
void CCMainWindow::drawButtonIcon(QPushButton *& btn, const QString & icon)
{
QPixmap pixmap(btn->size()); //这是画布
pixmap.fill(Qt::transparent); //透明填充
QPixmap iconPixmap(icon); //原片
QPainter p(&pixmap);
//绘画起点
p.drawPixmap((pixmap.width() - iconPixmap.width()) / 2, (pixmap.height() - iconPixmap.height()) / 2, iconPixmap);
btn->setIcon(pixmap);
btn->setIconSize(btn->size());
}
函数调用:
void CCMainWindow::initButton()
{
//状态按钮
drawButtonIcon(ui.statusBtn, BTN_STATUX_OK_PNG);
}