Qt Utilities  6.4.1
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
iconbutton.h
Go to the documentation of this file.
1 #ifndef WIDGETS_ICONBUTTON_H
2 #define WIDGETS_ICONBUTTON_H
3 
4 #include "../global.h"
5 
6 #include <QAbstractButton>
7 #include <QAction>
8 #include <QPixmap>
9 #include <QSize>
10 
11 #include <cstdint>
12 
13 namespace QtUtilities {
14 
15 class QT_UTILITIES_EXPORT IconButton : public QAbstractButton {
16  Q_OBJECT
17  Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
18 
19 public:
20  explicit IconButton(QWidget *parent = nullptr);
21  ~IconButton() override;
22 
23  static IconButton *fromAction(QAction *action, std::uintptr_t id = 0);
24  const QPixmap &pixmap() const;
25  void setPixmap(const QPixmap &pixmap);
26  QSize sizeHint() const override;
27 
28  static constexpr auto defaultPixmapSize = QSize(16, 16);
29 
30 protected:
31  void paintEvent(QPaintEvent *event) override;
32  void keyPressEvent(QKeyEvent *event) override;
33  void keyReleaseEvent(QKeyEvent *event) override;
34 
35 private Q_SLOTS:
36  void assignDataFromActionChangedSignal();
37  void assignDataFromAction(const QAction *action);
38 
39 private:
40  QPixmap m_pixmap;
41 };
42 
46 inline const QPixmap &IconButton::pixmap() const
47 {
48  return m_pixmap;
49 }
50 
54 inline void IconButton::setPixmap(const QPixmap &pixmap)
55 {
56  m_pixmap = pixmap;
57  update();
58 }
59 } // namespace QtUtilities
60 
61 #endif // WIDGETS_ICONBUTTON_H
A simple QAbstractButton implementation displaying a QPixmap.
Definition: iconbutton.h:15
void setPixmap(const QPixmap &pixmap)
Sets the pixmap.
Definition: iconbutton.h:54
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.