How to set image on QPushButton?

前端 未结 8 439
时光说笑
时光说笑 2020-11-30 21:34

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 QL

8条回答
  •  难免孤独
    2020-11-30 22:10

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

提交回复
热议问题