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

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 plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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.   
  8.        .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;}  
  9.        .triangle-right:before{position:absolute; content:"";display:block;}  
  10.        .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;}  
  11.        .triangle-right:after{position:absolute; content:"";display:block;}  
  12.        .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;}  
  13.   
  14.        .triangle-time{text-align:center;color:#999;font-size:12px; font-family:Arial;clear:both;margin:5px 0;}  
  15.        img{max-width:300px;margin:5px 0;}  
  16. </style>  

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

  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

  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.   
  37.     ui->webView->setHtml(messageList+"<a id='butt'></a></div></body>",  
  38.                          QUrl(QCoreApplication::applicationDirPath()+"//"));  
  39.   
  40.     QWebFrame* mf = ui->webView->page()->mainFrame();  
  41.     mf->scrollToAnchor("butt");  
  42.     imgLst.clear();  
  43. }  
这里主要参考了网上的代码,在此声明下 O(∩_∩)O~

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

看下效果图


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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值