timewidget.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef TIMEWIDGET_H
  2. #define TIMEWIDGET_H
  3. /**
  4. * 使用说明
  5. * 1、这个时间选择器支持两种方式
  6. * * 提升QTimeEdit,作为编辑栏修改时间
  7. * * 以弹窗的方式出现
  8. * 2、时间选择区域m_wdgTimeArea和时间编辑栏(在UI中)不是统一的,时间选择区域可以点击时间编辑栏
  9. * 创建出来,也可以在弹窗中直接显示出来,所以moveEvent事件就是用来移动m_wdgTimeArea的。
  10. * 3、在原来的基础上新添加了两个信号,在关闭的时候发送
  11. * 4、使用Dialog模式的时候,点击空白处隐藏就会close掉,然后发送新的时间信号
  12. * 5、使用Dialog模式,记得要调用showTimeEditArea()函数才会显示
  13. */
  14. #include <QFrame>
  15. #include <QTime>
  16. class TimePartWidget;
  17. class QListWidgetItem;
  18. class ShadowWidget;
  19. namespace Ui {
  20. class TimeWidget;
  21. }
  22. /* 添加ShowType类型,判断这个控件是编辑栏还是弹窗 */
  23. class TimeWidget : public QFrame
  24. {
  25. Q_OBJECT
  26. public:
  27. enum ShowType{
  28. EditLine = 0, /* 时间编辑栏 */
  29. Dialog = 1, /* 以弹窗的形式出现 */
  30. };
  31. explicit TimeWidget(QWidget *parent = nullptr , ShowType type = EditLine);
  32. ~TimeWidget();
  33. void CreateTimeVector(const QVector<int>& types);
  34. void ClearVector(QVector<TimePartWidget*>& vec);
  35. // 在父窗口无法容纳控件时,这是必要的
  36. void SetMainWindow(QWidget* pWidget);
  37. QString getTime();
  38. QTime getFormTime() const;
  39. void setTime(const QString& t);
  40. void setTime(const QTime& t);
  41. void clearTime();
  42. QString tipText() const;
  43. void setTimeAreaWidth(int w); /* 新增一个设置时间条宽度的函数 */
  44. /***** 2024-05-25 添加两个信号 ******/
  45. void showTimeEditArea();
  46. signals:
  47. void signal_nowTime(const QTime& time);
  48. void signal_formerTimer(const QTime& time);
  49. protected:
  50. bool eventFilter(QObject* obj, QEvent* e) override;
  51. void moveEvent(QMoveEvent *event) override;
  52. private slots:
  53. void onBtnTipClicked();
  54. void onListItemClicked(QListWidgetItem* item);
  55. void onDateTimeChanged(const QDateTime& dt);
  56. private:
  57. void UpdateProperty(QObject* obj, const char* name, bool flag);
  58. void UpdatePopupTime(const QDateTime& dt);
  59. void ShowTimeArea(bool bShow);
  60. void CreatePopupWidget();
  61. private:
  62. const int TIME_AREA_WIDTH = 56;
  63. const int TIME_AREA_HEIGHT = 32;
  64. const int SHADOW_MARGIN = 9; // 对应BlurRadius模糊半径16px
  65. Ui::TimeWidget *ui;
  66. QVector<TimePartWidget*> m_vecTimePart;
  67. QVector<int> m_vecTimeSections;
  68. bool m_bTimeFlag{false}; // 时间更新标志
  69. QScopedPointer<ShadowWidget> m_wdgTimeArea; // 时间选择窗口
  70. QWidget* m_pMainWindow; // 外层祖辈窗口,能容纳时间控件高度即可(默认父窗口)
  71. ShowType m_type; /* 显示类型 */
  72. int m_width = 0; /* TimeArea宽度 */
  73. };
  74. #endif // TIMEWIDGET_H