tags:

views:

63

answers:

3

I want to set images on QpushButton,and size of QPushButton depend upon size of image.

I am able to do this when using QLAbel,but not with QPushButton.

so,if anyone has any solution then plz..help me out.

A: 

I don't think you can set arbitrarily sized images on any of the existing button classes. If you want a simple image behaving like a button, you can write your own QAbstractButton-subclass, something like:

class ImageButton : public QAbstractButton {
Q_OBJECT
public:
...
    void setPixmap( const QPixmap& pm ) { m_pixmap = pm; update(); }
    QSize sizeHint() const { return m_pixmap.size(); }
protected:
    void paintEvent( QPaintEvent* e ) {
        QPainter p( this );
        p.drawPixmap( 0, 0, m_pixmap );
    }
};
Frank
Thanks for answering .. Will you plz help me by showing how can I implement this code by the use of QPushButton instead of QAbstractButton.As my requirements are to use QPushButton.so, plz help me out.
greshi Gupta
As I said, I don't think there is a proper way. If at all, QStyle and style sheets might help. Have a look there.
Frank
thanks,for answering.
greshi Gupta
A: 

there are two ways using which you can set the image on a button in Qt, Programmatic way of setting image,

http://qt-articles.blogspot.com/2010/06/how-to-customize-button-in-qt.html

from style sheet how the image is being set,

http://qt-articles.blogspot.com/2010/06/how-to-add-stylesheet-for-button-in-qt.html

Shadow
thats great...it works fine..thanks alot...
greshi Gupta
You are most welcome :)if you feel the answer is correct, please mark it as right so it would helpful for other who gets the similar problem.
Shadow
A: 

What you can do is use a pixmap as an icon and then put this icon onto the button.

To make sure the size of the button will be correct, you have to reisze the icon according to the pixmap size.

Something like this should work :

QPixmap pixmap("image_path");
QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());
Jérôme