Qt实现屏幕底部冒泡效果的方法

  介绍

这篇文章主要介绍Qt实现屏幕底部冒泡效果的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

在Qt局域网聊天程序的到的东西太多了,最想和大家分享的是关于局域网聊天信息的冒泡,他的设计也不是特别难,我写了一个类分享给大家。

可能各位道友有更好的办法,但希望不要拆台哦。

该类中有一部分适应屏幕分辨率的类,也一并加入。如有不懂的欢迎大家留言。

 Qt实现屏幕底部冒泡效果的方法

首先是获取屏幕分辨率,并实现自适应屏幕分辨率。

的ifndef VERDESKTOP_H
  #定义VERDESKTOP_H
  
  # include & lt; QObject>
  # include & lt; QDesktopWidget>
  
  类VerDesktop:公共QObject
  {
  Q_OBJECT
  公众:
  明确VerDesktop (QObject *父=0);
  浮动getVerDesktop ();
  
  信号:
  
  公共槽:
  };
  
  # endif//VERDESKTOP_H 
 # include“verdesktop.h"
  
  VerDesktop: VerDesktop (QObject *父母):QObject(父)
  {
  }
  浮动VerDesktop: getVerDesktop ()
  {
  QDesktopWidget dw;
  .width浮动版本=浮动(dw.screenGeometry()())/浮动(1920);
  返回版本;
  }

以下是冒泡的代码

的ifndef MESSAGEDIALOG_H
  #定义MESSAGEDIALOG_H
  # include & lt; QDialog>
  # include & lt; QPropertyAnimation>
  # include & lt; QLabel>
  # include“verdesktop.h"
  # include & lt; QTimer>
  # include & lt; QPaintEvent>
  # include & lt; QPainter>
  
  
  类MessageDialog:公共QDialog
  {
  Q_OBJECT
  公众:
  浮动版本;
  VerDesktop * v;//适应屏幕分辨率
  明确MessageDialog (QWidget *父=0);
  QLabel * imagelabel;//头像
  QLabel * namelabel;//名
  空白messagedialogseting();//设置
  QTimer * timer0;
  私人:
  空白paintEvent (QPaintEvent *事件);
  
  公共槽:
  空白timerout ();
  };
  
  # endif//MESSAGEDIALOG_H 
 # include“messagedialog.h"
  # include & lt; QApplication>
  # include & lt; QDesktopWidget>
  # include & lt; QGraphicsDropShadowEffect>
  # include & lt; QPalette>
  
  MessageDialog: MessageDialog (QWidget *父母):QDialog(父)
  {
  v=new VerDesktop(这个);
  版本=v→getVerDesktop ();
  messagedialogseting ();
  连接(timer0信号(超时()),槽(timerout ()));
  }
  空白MessageDialog: messagedialogseting()//显现的动画
  {
  QPalette调色板(这→调色板());
  palette.setColor (QPalette::背景、QColor (49225215);
  setPalette(面板);
  setAutoFillBackground(真正的);
  setWindowFlags (Qt:: FramelessWindowHint | windowFlags ());
  QRect矩形=QApplication::桌面()→availableGeometry ();
  setGeometry (rect.width() -250 *版本,rect.height() -80 *版本,250 *版本,80 *版本);
  QGraphicsDropShadowEffect *效果=new QGraphicsDropShadowEffect(这个);
  影响→setOffset (10,10);
  影响→setBlurRadius (10);
  影响→改变颜色(QColor (50 50,50));
  这→setGraphicsEffect(效应);
  imagelabel=new QLabel(这个);
  namelabel=new QLabel(这个);
  imagelabel→setGeometry(15 * 5 *版本,版本,50 *版本,50 *版本);
  namelabel→setGeometry(90 *版本,0150 *版本,80 *版本);
  namelabel→setFont (QFont(“微软雅黑“,15 *版本));
  namelabel→setAlignment (Qt:: AlignCenter);
  QPropertyAnimation * animation0=new QPropertyAnimation(这一点,“geometry");
  animation0→setDuration(500 *版本);
  animation0→setStartValue (QRect (rect.width (), rect.height() -80 *版本,250 *版本,80 *版本));//起点
  animation0→setEndValue (QRect (rect.width() -250 *版本,rect.height() -80 *版本,250 *版本,80 *版本));//终点
  animation0→开始(QAbstractAnimation:: DeleteWhenStopped);
  timer0=new QTimer(这个);
  timer0→开始(1000);
  }
  空白MessageDialog: timerout()//消失的动画
  {
  timer0→停止();
  QPropertyAnimation * animation0=new QPropertyAnimation(这一点,“windowOpacity");
  animation0→setDuration (500);
  animation0→setStartValue (1);
  animation0→setEndValue (0);
  animation0→开始(QAbstractAnimation:: DeleteWhenStopped);
  连接(animation0信号(完成()),槽(close ()));
  }
  空白MessageDialog: paintEvent (QPaintEvent *事件)//做阴影
  {
  const int x=3;
  Q_UNUSED(事件);
  QPainterPath yinying_path;
  yinying_path.setFillRule (Qt:: WindingFill);
  yinying_path.addRect (x, x,这→宽度()2 * x,这→高度()2 * x);
  QPainter画家(这个);
  painter.setRenderHint (QPainter:抗锯齿,真实);
  QColor颜色(0,0,0,50);
  for (int i=0; i

Qt实现屏幕底部冒泡效果的方法