ToolTip.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef TOOLTIP_H
  2. #define TOOLTIP_H
  3. #include <QFrame>
  4. #include <QLabel>
  5. class ToolTip : public QFrame
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit ToolTip(const QString &text = "", QWidget *parent = nullptr);
  10. QString text() const;
  11. void setText(const QString &text);
  12. int duration() const;
  13. void setDuration(int duration);
  14. void adjustPos(const QPoint &pos, const QSize &size);
  15. // QWidget interface
  16. protected:
  17. virtual void showEvent(QShowEvent *event) override;
  18. virtual void hideEvent(QHideEvent *event) override;
  19. private:
  20. void setQss();
  21. signals:
  22. private:
  23. QString m_text;
  24. int m_duration;
  25. QFrame *m_container;
  26. QTimer *m_timer;
  27. QLabel *m_label;
  28. };
  29. class ToolTipFilter : public QObject
  30. {
  31. Q_OBJECT
  32. public:
  33. /**
  34. * @brief ToolTipFilter
  35. * @param parent: the widget to install tool tip
  36. * @param showDelay: show tool tip after how long the mouse hovers in milliseconds
  37. */
  38. explicit ToolTipFilter(QWidget *parent, int showDelay = 300);
  39. void hideToolTip();
  40. void showToolTip();
  41. void setTooltipDelay(int delay);
  42. // QObject interface
  43. public:
  44. virtual bool eventFilter(QObject *watched, QEvent *event) override;
  45. private:
  46. bool m_isEnter;
  47. ToolTip *m_toolTip;
  48. int m_tooltipDelay;
  49. QTimer *m_timer;
  50. };
  51. #endif // TOOLTIP_H