当前位置:编程学习 > wap >>

怎么将图片把PushButton填满

 
    QPixmap Face_png = QPixmap();
    Face_png.load("D:/1.png");
    QPixmap Face_png1 = QPixmap("D:/2.png");//.scaled(300,30);
    QPushButton *FaceLab_1 = new QPushButton(Face_png,"AAA",&w);


程序界面


[img=http://hi.csdn.net/attachment/201111/20/0_1321784987ADKA.gif ] [/img]    资源图片 --------------------编程问答-------------------- 用stylesheet --------------------编程问答-------------------- style sheet: 
border-image --------------------编程问答--------------------

FaceLab_1->setStyleSheet("border-image:url(:/images/media_bj.jpg)");
--------------------编程问答-------------------- 就是这样
引用 3 楼 prochsh 的回复:
C/C++ code


FaceLab_1->setStyleSheet("border-image:url(:/images/media_bj.jpg)");
--------------------编程问答-------------------- 1.用border-image
2.设定icon size --------------------编程问答-------------------- --------------------编程问答--------------------


#pragma once
#include <QPushButton>
#include <QImage>
#include <QPixmap>
#include <QSize>
#include <QIcon>
#include <QPaintEvent>
#include <QPainter>
#include <QColor>
class IButton :
    public QPushButton
{
    Q_OBJECT
public:
    IButton(QWidget * parent = 0);
    ~IButton(void);
    void setBlack(QString & name);
    void wordshow(QString &show_word);
    void set_font_size();
public:
    QString word_;
protected:
    QImage *image_;
    QIcon *icon_;
    QPixmap *pixmap_;
protected:
    void paintEvent( QPaintEvent * event );
};




#include "IButton.h"

IButton::IButton(QWidget * parent)
    :QPushButton(parent)
{
    int h;
    QPalette palette_;// = palette();
    palette_ = palette();
    
    QFont font_ = font();
    
    image_ = new QImage();
    pixmap_ = new QPixmap();
    icon_ = new QIcon();
    word_.clear();
    h = size().width()>size().height()?size().height():size().width();
    font_.setPointSize(h-h/3);
    setFont(font_);
    palette_.setColor(QPalette::Active,QPalette::ButtonText,QColor(255,255,255));
    setPalette(palette_);
    setFocusPolicy(Qt::NoFocus);    
}

IButton::~IButton(void)
{
    delete []image_;
    delete []pixmap_;
    delete icon_;
}
void IButton::setBlack(QString & name)
{
    image_->load(name);
    QSize s = size();//the button you would to set the back;
    QImage i2 = image_->scaled(s);//adapt the size of button;
    *pixmap_ = QPixmap::fromImage(i2);//this is the static function
    setIcon(QIcon(*pixmap_));
    setIconSize(s);
}
void IButton::paintEvent(QPaintEvent * event)
{
    QPushButton::paintEvent(event) ;
    QPainter painter(this) ; 
    if (!word_.isEmpty())
    {
        painter.drawText(rect(),Qt::AlignCenter,word_);
    }    
    
    
    event->accept();
    return ;
}
void IButton::wordshow(QString &show_word)
{
    word_ = show_word;
}
void IButton::set_font_size()
{

}

上面是没写完的封装,可以使你的背景图片充满整个BUTTON还可以再BUTTON上面显示文字。但有个巨大的BUG
就是内存问题。这个问题我问了2个星期了,没人鸟我。我自己也没解决。问题出现在setIcon 中
如果楼主采用 setIcon的方法如果没有出现问题,记得和我联系下,教教我。如果出现同样问题,一起探讨下
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,