前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Qt的类QQ气泡聊天的界面开发(二)

基于Qt的类QQ气泡聊天的界面开发(二)

作者头像
bear_fish
发布2018-09-20 12:21:48
1.9K0
发布2018-09-20 12:21:48
举报

http://blog.csdn.net/esonpo/article/details/25974999

1:使用Qt下面的QListview来实现QQ类似效果,差强人意

2:使用QWebview加载html css样式来完成,发现效果不错,但是毕竟webview占用巨大的内存

3:使用QTextBrower加载css,但是好像只支持css2.1版本,css3完全不支持,这样的话,花哨的样式应该是无法实现

前章写了使用delegate实现气泡效果,但是始终无法做到与QQ匹敌的效果,好多功能不能实现

现在使用第二种方法

   第二种方式主要是使用QWebview来加载css样式来实现功能,

  首先需要写好css样式,新建一个css文件,里面只写css样式

[html] view plaincopyprint?

  1. <style type="text/css">
  2. .triangle-left{float:left;max-width:300px;border:1px solid #b6b6b7; border-radius:10px;padding:15px;background:#efeff0;margin:0  0 15px 25px; position:relative; display:inline-block;box-shadow:0 4px 8px #888888;word-wrap: break-word;}  
  3.        .triangle-left:before{position:absolute; content:"";display:block;}  
  4.        .triangle-left.left:before{border-color:rgba(0, 0, 0, 0) #B6B6B7 rgba(0, 0, 0, 0) rgba(0, 0, 0, 0);border-width:8px 19px 10px 40px;border-style:solid;bottom:auto;left:-60px;top:8px;}  
  5.        .triangle-left:after{position:absolute; content:"";display:block;}  
  6.        .triangle-left.left:after{border-color:rgba(0, 0, 0, 0) #efeff0 rgba(0, 0, 0, 0) rgba(0, 0, 0, 0);border-width:8px 19px 10px 40px;border-style:solid;bottom:auto;left:-57px;top:8px;}  
  7.        .triangle-right{float:right;max-width:300px;border:1px solid #b6b6b7;border-radius:10px;padding:15px;background:#d3edf9;position:relative;display:inline-block;margin:0 25px 15px 0;box-shadow:0 4px 8px #888888;word-wrap: break-word;}  
  8.        .triangle-right:before{position:absolute; content:"";display:block;}  
  9.        .triangle-right.right:before{border-color:rgba(0, 0, 0, 0) rgba(0, 0, 0, 0) rgba(0, 0, 0, 0) #B6B6B7;border-width:8px 40px 10px 19px;border-style:solid;bottom:auto;right:-60px;top:8px;}  
  10.        .triangle-right:after{position:absolute; content:"";display:block;}  
  11.        .triangle-right.right:after{border-color:rgba(0, 0, 0, 0) rgba(0, 0, 0, 0) rgba(0, 0, 0, 0) #d3edf9;border-width:8px 40px 10px 19px;border-style:solid;bottom:auto;right:-57px;top:8px;}  
  12.        .triangle-time{text-align:center;color:#999;font-size:12px; font-family:Arial;clear:both;margin:5px 0;}  
  13.        img{max-width:300px;margin:5px 0;}  
  14. </style>

然后C++代码里面new 一个QWebview实例,去加载这段css文件

[cpp] view plaincopyprint?

  1. QFile file(":/send.html");  
  2. if(!file.open(QIODevice::ReadOnly))  
  3.     {  
  4. //chatStyle.html doesn't exsit
  5.         messageList="";  
  6.     }  
  7. else
  8.     {  
  9.         messageList = file.readAll();  
  10.         messageList += "<div class=\"container\">";  
  11.         ui->webView->setHtml(messageList);  
  12.     }  
  13.     file.close();  

初始化后接收发的消息然后加入webview内显示,这里只要是拼装html+css

[cpp] view plaincopyprint?

  1. void YibanChat::AddMessageToList(QString mcontent,   //消息内容
  2.                                  QString authorName, //消息发送者昵称
  3. int senderType,QStringList imgList)     //发送者身份
  4. {  
  5.     qDebug()<<"mcontent======================>  "<<imgList;  
  6. for(int i=0;i<imgList.count();i++){  
  7. if(imgList.at(i).isEmpty()){  
  8. break;  
  9.         }  
  10.         QImage fileInfo(imgList.at(i));  
  11.         QSize iW =  fileInfo.size();  
  12. if(iW.width()>200 || iW.height()>200){  
  13.             mcontent.append("<img src=\""+imgList.at(i)+"\" width=\"200\" height=\"200\"\" /> ");  
  14.         }else{  
  15.             mcontent.append("<img src=\""+imgList.at(i)+"\" width=\""+iW.width()+"\" height=\""+iW.height()+"\" /> ");  
  16.         }  
  17.     }  
  18.     QString strTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");  
  19. if(senderType < 0){  
  20.         messageList+=("<p>"
  21. "<div style=\"text-align:left;padding-left:150px\">"+strTime+"</div></p>");  
  22.         messageList+=("<p class=\"Me\"></p><p class=\"triangle-right\"><strong>[");  
  23.     }else if(senderType > 0){  
  24.         messageList+=("<p>"
  25. "<div style=\"text-align:left;padding-left:150px\">"+strTime+"</div></p>");  
  26.         messageList+=("<p class=\"U\"></p><p class=\"triangle-left\">[<strong>");  
  27.     }else{  
  28.         messageList+=("<p class=\"root\"></p><p class=\"rootsaid\">[<strong>");  
  29.     }  
  30.     messageList+=authorName;  
  31.     messageList+=tr("]:</strong></br>");  
  32.     messageList+=mcontent;  
  33.     messageList+=tr("</p><div class=\"clear\"></div>");  
  34.     ui->webView->setHtml(messageList+"</div></body>",  
  35.                          QUrl(QCoreApplication::applicationDirPath()+"//"));  
  36.     ui->webView->setHtml(messageList+"<a id='butt'></a></div></body>",  
  37.                          QUrl(QCoreApplication::applicationDirPath()+"//"));  
  38.     QWebFrame* mf = ui->webView->page()->mainFrame();  
  39.     mf->scrollToAnchor("butt");  
  40.     imgLst.clear();  
  41. }  

这里主要参考了网上的代码,在此声明下 O(∩_∩)O~

我之后实现了添加图片和截图发送的功能而已

看下效果图

但是这个只支持单窗口聊天,要是每个聊天窗口都实例一个webview的话,内存吃不消,有人提到使用一个webview,然后写个html实现多个tab切换和新建、删除等功能,由于html只是有限,遂未做研究,希望有心之人可以实现之。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年05月15日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档