qtutilities/widgets/iconbutton.h

62 lines
1.3 KiB
C
Raw Permalink Normal View History

2015-04-22 18:57:44 +02:00
#ifndef WIDGETS_ICONBUTTON_H
#define WIDGETS_ICONBUTTON_H
#include "../global.h"
2015-04-22 18:57:44 +02:00
#include <QAbstractButton>
#include <QAction>
2015-04-22 18:57:44 +02:00
#include <QPixmap>
#include <QSize>
2015-04-22 18:57:44 +02:00
#include <cstdint>
namespace QtUtilities {
2015-04-22 18:57:44 +02:00
2017-05-01 03:16:25 +02:00
class QT_UTILITIES_EXPORT IconButton : public QAbstractButton {
2015-04-22 18:57:44 +02:00
Q_OBJECT
Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
public:
explicit IconButton(QWidget *parent = nullptr);
2018-10-10 21:12:58 +02:00
~IconButton() override;
2015-04-22 18:57:44 +02:00
static IconButton *fromAction(QAction *action, std::uintptr_t id = 0);
2015-04-22 18:57:44 +02:00
const QPixmap &pixmap() const;
void setPixmap(const QPixmap &pixmap);
2018-10-10 21:12:58 +02:00
QSize sizeHint() const override;
2015-04-22 18:57:44 +02:00
static constexpr auto defaultPixmapSize = QSize(16, 16);
2015-04-22 18:57:44 +02:00
protected:
2018-10-10 21:12:58 +02:00
void paintEvent(QPaintEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
2015-04-22 18:57:44 +02:00
private Q_SLOTS:
void assignDataFromActionChangedSignal();
void assignDataFromAction(const QAction *action);
2015-04-22 18:57:44 +02:00
private:
QPixmap m_pixmap;
};
/*!
* \brief Returns the pixmap.
*/
inline const QPixmap &IconButton::pixmap() const
{
return m_pixmap;
}
/*!
* \brief Sets the pixmap.
*/
inline void IconButton::setPixmap(const QPixmap &pixmap)
{
m_pixmap = pixmap;
update();
}
} // namespace QtUtilities
2015-04-22 18:57:44 +02:00
#endif // WIDGETS_ICONBUTTON_H