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