zoukankan      html  css  js  c++  java
  • Qt实现长文件名(字符串)在QLabel中自适应缩短

    一、应用场景简述

          当在有限宽度的QLable中显示很长的文件名/字符串时,超出QLabel宽度部分将不会显示,此时采取缩短文件名策略(也可实现为字符串滚动动画)可以缓解这一问题。在实现这一想法的过程中,先后提出两个解决方案。遂再次分享给大家。

    二、方案实现

    //方案1:简单的保留前面几个字符,去出中间的几个字符,保留后面几个字符

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. QString scalText(QString org)  
    2. {  
    3.     QString result;  
    4.     const quint16 strLen = org.length();  
    5.     int index = org.lastIndexOf(".");  
    6.     if(-1 == index){ //如果返回-1表示没找到  
    7.         //无后缀名  
    8.         QString fileName = org;  
    9.         if( strLen < 11)  
    10.             result = fileName;  
    11.         else{  
    12.             result = fileName.mid(0,3); //取前三个字符  
    13.             result += "...";  
    14.             result = fileName.mid(strLen-6,6); //取后6个字符  
    15.         }  
    16.     }  
    17.     else{  
    18.         //有后缀名  
    19.         if( strLen < 11){  
    20.             result = org;  
    21.         }  
    22.         else{  
    23.             const QString fileName = org.left(index); //文件名  
    24.             const quint16 fileNameLen = fileName.length();  
    25.             const QString fileExtName = org.right(strLen - 1 - org.lastIndexOf(".")); //文件扩展名  
    26.             result = fileName.mid(0,3);  
    27.             result += "..." + fileName.mid(fileNameLen-3,3); //取后3个字符  
    28.             result += "." + fileExtName;      //追加后缀名  
    29.         }  
    30.     }  
    31.     return result;  
    32. }  

          此实现方案灵活性差,通用性差,而且在文件名/字符串中混合这ASCII码和宽字符时显示效果极差。

    //方案2:根据QLabel的实际宽度、字体的尺寸属性等对文件名进行缩短

    //目标: 将“长文件名测试文件-长文件名测试文件-长文件名测试文件.wmv 1203MB”缩短为“长文件名...测试文件.wmv 1203MB”

    //定义: QString scalText(QString org, unsigned int showWidth ,QString arg1="");

    //参数:org-待缩短的字符串/文件名,如上面的“长文件名测试文件-长文件名测试文件-长文件名测试文件.wmv”

    //            showWidht-QLabel的实际宽度

    //            arg1-追加到org后面的补充字符串

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. QString scalText(QString org, unsigned int showWidth ,QString arg1)  
    2. {  
    3.     QString result;  
    4.     QString chngeStr("...");  
    5.     QFontMetrics fm(QFont("微软雅黑",10));  
    6.     const unsigned int labWidthPxs = showWidth-10; //label的固定宽度  
    7.     int textWidthInPxs = fm.width(org);  
    8.     int unitsWidthPxs = fm.width(arg1);  
    9.     int blankWidthPxs = fm.width(" ");  
    10.     int chngeWidthPxs = fm.width(chngeStr);  
    11.     unsigned int remainWidthPxs = labWidthPxs - unitsWidthPxs - blankWidthPxs - chngeWidthPxs;  
    12.   
    13.     if(textWidthInPxs < remainWidthPxs){  
    14.         result = org;  
    15.     }  
    16.     else{  
    17.         short preIndex = 4, rearIndex =4;  //保留前4个字符  
    18.         int pickUpWidthPxs = 0;  
    19.         do{  
    20.             ++rearIndex;  
    21.             QString pickUp = org.mid(preIndex,rearIndex-preIndex);  
    22.             pickUpWidthPxs = fm.width(pickUp);  
    23.             QString preFix = org.mid(0,preIndex);  
    24.             QString sufFix = org.mid(rearIndex, org.length()-rearIndex);  
    25.             result = preFix + chngeStr + sufFix;  
    26.         }while(textWidthInPxs-pickUpWidthPxs > remainWidthPxs);  
    27.     }  
    28.     return result;  
    29. }  
     

    测试:

    ui->label.setText(scalText(fileName, ui->label.Width(), "1023MB");

    三、更新

    第一次更新  2016-09-05

    http://blog.csdn.net/qq2399431200/article/details/52438562

  • 相关阅读:
    实例、数据库和表空间(转载)
    异步邮件阻塞MVC请求
    发布Asp.Net MVC 注意事项
    CWebBrowser2 图片缩放,点击小图看大图
    Web服务器与客户端时差问题
    一些好用的eclipse插件
    ASP.NET Deployment and Configuration Problems
    第二届游戏开发者大会
    MVC 请求参数中带有HTML会引发Validation异常 ("A potentially dangerous Request.Form value was detected from the client")
    网络语言标准实施规范 ISO2009
  • 原文地址:https://www.cnblogs.com/findumars/p/6568622.html
Copyright © 2011-2022 走看看