Qt 自定义悬浮窗(带动画,类似QQ拼音输入法)

小灰灰 2023-07-03 06:29 63阅读 0赞

1.运行效果

20200208222435355.gif

实现功能:

1.可拖动。

2.可显示,可隐藏 。

3.悬浮在主界面上面。

4.带动画。

2.ui界面

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d6ejk1MzIwMDQ2Mw_size_16_color_FFFFFF_t_70

3.源码

  1. //FloatingWindow.h
  2. #pragma once
  3. #include <QWidget>
  4. #include "ui_FloatingWindow.h"
  5. #include <QPoint>
  6. #include <QPropertyAnimation>
  7. class FloatingWindow : public QWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. FloatingWindow(QWidget *parent = Q_NULLPTR);
  12. ~FloatingWindow();
  13. private:
  14. Ui::FloatingWindow ui;
  15. protected:
  16. //事件过滤
  17. bool eventFilter(QObject *watched, QEvent *event);
  18. private:
  19. void hideWidget(QWidget *w = nullptr);
  20. void showWidget(QWidget *w = nullptr);
  21. private:
  22. bool m_bDragFlag;
  23. QPoint m_pointDrag;
  24. QPropertyAnimation *m_pAnimation;
  25. QPropertyAnimation *m_pAnimation2;
  26. };
  27. #endif // FLOATINGWINDOW_H
  28. //FloatingWindow.cpp
  29. #include "FloatingWindow.h"
  30. #include <QMouseEvent>
  31. FloatingWindow::FloatingWindow(QWidget *parent)
  32. : QWidget(parent)
  33. , m_bDragFlag(false)
  34. , m_pAnimation(nullptr)
  35. , m_pAnimation2(nullptr)
  36. {
  37. ui.setupUi(this);
  38. setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
  39. ui.btnQQ->installEventFilter(this);
  40. ui.widget->installEventFilter(this);
  41. hideWidget();
  42. m_pAnimation = new QPropertyAnimation(ui.widget, "size");
  43. m_pAnimation->setDuration(250);
  44. m_pAnimation->setStartValue(QSize(0, 64));
  45. m_pAnimation->setEndValue(QSize(180, 64));
  46. m_pAnimation->setEasingCurve(QEasingCurve::Linear);
  47. m_pAnimation2 = new QPropertyAnimation(ui.widget, "size");
  48. m_pAnimation2->setDuration(250);
  49. m_pAnimation2->setStartValue(QSize(180, 64));
  50. m_pAnimation2->setEndValue(QSize(0, 64));
  51. m_pAnimation2->setEasingCurve(QEasingCurve::Linear);
  52. setAutoFillBackground(false);
  53. setAttribute(Qt::WA_TranslucentBackground);
  54. ui.frame->setStyleSheet("background:transparent;");
  55. }
  56. FloatingWindow::~FloatingWindow()
  57. {
  58. }
  59. bool FloatingWindow::eventFilter(QObject *target, QEvent *event)
  60. {
  61. QMouseEvent *mouse = dynamic_cast<QMouseEvent *>(event);
  62. if (target == ui.btnQQ)
  63. {
  64. if (event->type() == QEvent::Enter)
  65. {
  66. if (m_pAnimation->state() != QAbstractAnimation::Running)
  67. {
  68. showWidget();
  69. m_pAnimation->start();
  70. }
  71. }
  72. if (event->type() == QEvent::Leave)
  73. {
  74. if (m_pAnimation2->state() != QAbstractAnimation::Running)
  75. {
  76. m_pAnimation2->start();
  77. }
  78. }
  79. }
  80. //拖动
  81. if (mouse)
  82. {
  83. if (mouse->button() == Qt::LeftButton && mouse->type() == QEvent::MouseButtonPress)
  84. {
  85. m_bDragFlag = true;
  86. if (target == ui.btnQQ)
  87. {
  88. m_pointDrag = ui.frame->mapToParent(ui.btnQQ->mapToParent(mouse->pos()));
  89. }
  90. }
  91. else if (mouse->type() == QEvent::MouseButtonRelease)
  92. {
  93. m_bDragFlag = false;
  94. }
  95. else if (m_bDragFlag && mouse->type() == QEvent::MouseMove)
  96. {
  97. this->move(mouse->globalPos() - m_pointDrag);
  98. }
  99. }
  100. return QWidget::eventFilter(target, event);
  101. }
  102. void FloatingWindow::hideWidget(QWidget *w)
  103. {
  104. if (w == nullptr)
  105. {
  106. ui.btnExpression->hide();
  107. ui.btnSetting->hide();
  108. ui.btnSemiangle->hide();
  109. ui.btnWord->hide();
  110. }
  111. else
  112. {
  113. w->hide();
  114. }
  115. }
  116. void FloatingWindow::showWidget(QWidget *w)
  117. {
  118. if (w == nullptr)
  119. {
  120. ui.btnWord->show();
  121. ui.btnSemiangle->show();
  122. ui.btnSetting->show();
  123. ui.btnExpression->show();
  124. }
  125. else
  126. {
  127. w->show();
  128. }
  129. }

4.完整代码

带动画的悬浮窗

发表评论

表情:
评论列表 (有 0 条评论,63人围观)

还没有评论,来说两句吧...

相关阅读