Qt学习教程之对话框消失动画效果

2020-01-06 19:14:38丽君


m_pAnimation->setPropertyName("geometry");

QRect startRect = rect();
startRect.moveTo(pos());
QRect stopRect = QRect(startRect.center(), QSize(0, 0));

m_pAnimation->setStartValue(startRect);
m_pAnimation->setEndValue(stopRect);

4、动画启动机制

使用定时器控制动画,当指定时间后启动动画,并且在动画完成后关闭窗口


void InitializeConnect()
{
 m_pAnimation = new QPropertyAnimation(this);
 m_pAnimation->setTargetObject(this);

 connect(m_pAnimation, &QPropertyAnimation::finished, this, &GMPOperateTip::close);

 connect(&m_StayTimer, &QTimer::timeout, this, [this]{
  m_pAnimation->setDuration(m_DurationTime);
  switch (m_eMode)
  {
  case AM_FADEOUT:
   FadeOut_p();
   break;
  case AM_FLYOUT:
   MoveOut();
   break;
  case AM_ZOOMIN:
   ZoomIn();
   break;
  default:
   ;
  }

  m_pAnimation->start();
 });
}

窗口显示时启动定时器,并且将窗口随机移动到屏幕一个位置


bool event(QEvent * e)
{
 if (e->type() == QEvent::Show)
 {
  //QPoint pos = parentWidget()->rect().center() - this->rect().center();
  int wrand = qrand() % (parentWidget()->rect().width() - this->rect().width());
  int hrand = qrand() % (parentWidget()->rect().height() - this->rect().width());
  move(QPoint(wrand, hrand));

  m_StayTimer.start(m_iStayDuration);
 }

 return __super::event(e);
}

5、阴影


void setShadowEnable(bool enable)
{
 if (!m_pShadow)
 {
  m_pShadow = new QGraphicsDropShadowEffect(this);
  m_pShadow->setColor(QColor(0, 0, 0, 85));
  m_pShadow->setBlurRadius(10);
  m_pShadow->setOffset(4, 4);
 }

 setGraphicsEffect(enable ? m_pShadow : nullptr);
}

6、着色

注释中的代码也可以进行着色,但是窗体的一些特殊样式不能完成,因此使用stylesheet来完成背景色修改


 static const QString c_szStyleSheet = "QWidget{background-color:%1;
          border:1px solid %2;border-top:0;border-bottom-left-radius:3px;
          border-bottom-right-radius:3px;background-image: url();}";