ChatGPT解决这个技术问题 Extra ChatGPT

How to set image on QPushButton?

qt

I want to set an image on QPushButton, and the size of QPushButton should depend on the size of the image. I am able to do this when using QLabel, but not with QPushButton.

So, if anyone has a solution, then please help me out.

there are two ways using which you can set the image on a button in Qt, Programmatic way of setting image, qt-articles.blogspot.com/2010/06/… from style sheet how the image is being set, qt-articles.blogspot.com/2010/06/…
thats great...it works fine..thanks alot...
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.

J
Jérôme

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

I know this is old, but I have done this, and the pushbutton icon is still the default one. I'm getting no errors and all my resource files seem to be loaded and my paths are good. Why does this not work for me?
u
user1119279
QPushButton *button = new QPushButton;
button->setIcon(QIcon(":/icons/..."));
button->setIconSize(QSize(65, 65));

I
Iuliu

You can also use:

button.setStyleSheet("qproperty-icon: url(:/path/to/images.png);");

Note: This is a little hacky. You should use this only as last resort. Icons should be set from C++ code or Qt Designer.


J
Jonathan

You may also want to set the button size.

QPixmap pixmap("image_path");
QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());
button->setFixedSize(pixmap.rect().size());

F
Frank Osterfeld

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

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.
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.
N
Nemeth Attila

You can do this in QtDesigner. Just click on your button then go to icon property and then choose your image file.


Do this with the base 'icon' setting, at least to see it working with your image, before trying to set the icon per state. Set the size of the icon to be that of your image, or at least large enough for your image, or it will not display.
This doesn't work as the "icon" part asks you to put on some themes. QT6.1. I thought it would open a dialog and I can choose an image file but not really.
J
JFP74

This is old but it is still useful, Fully tested with QT5.3.

Be carreful, example concerning the ressources path :

In my case I created a ressources directory named "Ressources" in the source directory project.

The folder "ressources" contain pictures and icons.Then I added a prefix "Images" in Qt So the pixmap path become:

QPixmap pixmap(":/images/Ressources/icone_pdf.png");

JF


the answer should provide more info how to use the QPixmap with the QPushButton
A
AAEM

Just use this code

QPixmap pixmap("path_to_icon");
QIcon iconBack(pixmap);

Note that:"path_to_icon" is the path of image icon in file .qrc of your project You can find how to add .qrc file here


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now