bubbletip.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef BUBBLETIP_H
  2. #define BUBBLETIP_H
  3. #include <QLabel>
  4. #include <QPropertyAnimation>
  5. #include <QQueue>
  6. #include <QTimer>
  7. #include <QWidget>
  8. class BubbleTip : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. enum Position { Top, Bottom, Left, Right, Center };
  13. enum Type { Info, Success, Warning, Error, Default };
  14. static void showTip(QWidget* target,
  15. const QString& text,
  16. Position position = Bottom,
  17. int duration = 0,
  18. Type type = Default,
  19. QWidget* parent = nullptr);
  20. static void hideAll();
  21. private:
  22. explicit BubbleTip(QWidget* target,
  23. const QString& text,
  24. Position position,
  25. int duration,
  26. Type type,
  27. QWidget* parent = nullptr);
  28. void paintEvent(QPaintEvent* event) override;
  29. bool eventFilter(QObject* watched, QEvent* event) override;
  30. void updatePosition();
  31. void startShowAnimation();
  32. void startHideAnimation();
  33. void adjustFollowingTips();
  34. static void adjustTipsForTarget(QWidget* target);
  35. private:
  36. QWidget* m_target;
  37. Position m_position;
  38. Type m_type;
  39. QLabel* m_label;
  40. QTimer* m_timer;
  41. int m_verticalOffset; // 新增:垂直偏移量
  42. static QQueue<BubbleTip*> m_queue;
  43. static QMap<QWidget*, QMap<BubbleTip::Position, QQueue<BubbleTip*>>> m_targetMap;
  44. };
  45. #endif // BUBBLETIP_H