tooltiplabel.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "tooltiplabel.h"
  2. #include "ui_tooltiplabel.h"
  3. #include <QtConcurrent/QtConcurrent>
  4. TooltipLabel::TooltipLabel(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::TooltipLabel)
  7. {
  8. ui->setupUi(this);
  9. effect.setOpacity(0);
  10. setGraphicsEffect(&effect);
  11. QWidget::show();
  12. isHidden = true;
  13. }
  14. TooltipLabel::~TooltipLabel()
  15. {
  16. delete ui;
  17. }
  18. void TooltipLabel::show()
  19. {
  20. isHidden = false;
  21. QtConcurrent::run([this](){
  22. const int iterations_num = 20;
  23. const int iteration_sleep = 10;
  24. for (int i = 0; i < iterations_num && qApp && !isHidden; ++i) {
  25. qreal opacity = double(i) / 20.0;
  26. effect.setOpacity(opacity);
  27. QMetaObject::invokeMethod(this, "updateOpacity", Qt::QueuedConnection, Q_ARG(qreal, opacity));
  28. QThread::msleep(iteration_sleep);
  29. }
  30. });
  31. }
  32. void TooltipLabel::hide()
  33. {
  34. isHidden = true;
  35. QtConcurrent::run([this](){
  36. const int iterations_num = 20;
  37. const int iteration_sleep = 10;
  38. for (int i = iterations_num - 1; i >= 0 && qApp && isHidden; --i) {
  39. qreal opacity = double(i) / 20.0;
  40. QMetaObject::invokeMethod(this, "updateOpacity", Qt::QueuedConnection, Q_ARG(qreal, opacity));
  41. QThread::msleep(iteration_sleep);
  42. }
  43. });
  44. }
  45. void TooltipLabel::updateText(QString text)
  46. {
  47. ui->label_small->setText(text);
  48. }
  49. void TooltipLabel::updateOpacity(double opacity)
  50. {
  51. effect.setOpacity(opacity);
  52. setGraphicsEffect(&effect);
  53. }