iconbutton.h 792 Bytes
Newer Older
Andreas Traczyk's avatar
Andreas Traczyk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#pragma once
#include <QAbstractButton>
#include <QPainter>

class IconButton : public QAbstractButton {
    Q_OBJECT;

    Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap);
    Q_PROPERTY(QString text READ text WRITE setText);

public:
    explicit IconButton(QWidget* parent = nullptr);

    QPixmap pixmap() {
        return pixmap_;
    }

    void setPixmap(const QPixmap& pm) {
        pixmap_ = pm;
        update();
    }

    QString text() const {
        return text_;
    }
    void setText(const QString& text) {
        text_ = text;
    }

protected:
    void enterEvent(QEvent* e) override;
    void leaveEvent(QEvent* e) override;
    void paintEvent(QPaintEvent* e);

    QSize sizeHint() const;

private:
    QPixmap pixmap_;
    QString text_;

    bool hovered_;
};