qml文本控件和多行文本显示省略号

qml Text控件专门用于显示非编辑文本,功能还是比较强大的。改变多行文本,需要修改wrapMode: Text.WrapAnywhereelide: Text.ElideRightmaximumLineCount:2即可

  1 import QtQuick 2.9
  2 import QtQuick.Window 2.2
  3 
  4 Window {
  5     visible: true
  6      640
  7     height: 480
  8     title: qsTr("Hello World")
  9     Text{
 10         id:text_test
 11 
 12                      200
 13 
 14                     anchors.horizontalCenter: parent.horizontalCenter
 15 
 16                     clip :true  //是否剪切掉超出显示范围的文字,默认false
 17 
 18                     text:"Hello Wo111111111111111111111"      //需要显示的文字
 19 
 20                     color: "red"            //文字颜色
 21 
 22                     font.family: "Corbel"   //字体
 23 
 24                     font.pixelSize: 25      //字体大小设置为像素
 25 
 26                     //font.pointSize: 100     //将字体的大小设置为点,存在pixelSize设置,本设置无效
 27 
 28                     font.bold: true         //是否加粗,默认为false
 29 
 30                     font.capitalization: Font.MixedCase //设置文本大小写,不使用大小写,默认值
 31 
 32                     //font.capitalization: Font.AllUppercase //全部使用大写
 33 
 34                     //font.capitalization: Font.AllLowercase   //全部使用小写
 35 
 36                     //font.capitalization: Font.SmallCaps       //小写字母使用小大写
 37 
 38                     //font.capitalization: Font.Capitalize        //第一个单词第一个字符大写
 39 
 40                     font.italic: true         //设置字体是否斜体样式,默认false
 41 
 42                     font.letterSpacing: 8    //设置字母之间的距离,正数为增加默认距离,负数为减少
 43 
 44                     //font.strikeout: true     //设置是否有删除线(中间一条线),默认false
 45 
 46                     font.underline: true     //设置是否有下滑线,默认false
 47 
 48                     //font.weight: Font.Light
 49 
 50                     //font.weight: Font.Normal
 51 
 52                     //font.weight: Font.DemiBold
 53 
 54                     //font.weight: Font.Bold
 55 
 56                     //font.weight: Font.Black
 57 
 58                     font.wordSpacing: 10      //设置单词之间的距离
 59 
 60                     //horizontalAlignment: Text.AlignRight //右对齐
 61 
 62                     //horizontalAlignment: Text.AlignLeft    //左对齐
 63 
 64                     //horizontalAlignment: Text.AlignHCenter   //中间对齐
 65 
 66                     horizontalAlignment: Text.AlignJustify
 67 
 68                     //verticalAlignment:   Text.AlignTop      //上对齐
 69 
 70                     verticalAlignment:   Text.AlignBottom     //下对齐
 71 
 72                     //verticalAlignment:   Text.AlignVCenter  //中间对齐
 73 
 74                     smooth:true        //是平滑
 75 
 76                     //style: Text.Normal  设置字体样式
 77 
 78                     //style: Text.Outline
 79 
 80                    // style: Text.Raised
 81 
 82                     //style: Text.Sunken
 83 
 84                     styleColor: "blue" //配合style使用
 85 
 86                     //textFormat: Text.AutoText //文本属性显示方式
 87 
 88                     //textFormat: Text.PlainText
 89 
 90                     //textFormat: Text.RichText
 91 
 92                     //textFormat: Text.StyledText
 93 
 94                     wrapMode: Text.WrapAnywhere   //换行属性设置,但需要明确宽度
 95 
 96                    // wrapMode: Text.WordWrap    //
 97 
 98                //wrapMode: Text.WrapAnywhere
 99 
100                     //wrapMode: Text.Wrap
101 
102                     elide: Text.ElideRight //超出显示范围的用...代替
103 
104                     //elide: Text.ElideNone
105 
106                     //elide: Text.ElideMiddle
107 
108                     // elide: Text.ElideLeft
109                     lineHeightMode:Text.FixedHeight  //行距
110                     lineHeight:20
111                     maximumLineCount:2
112 
113             onImplicitWidthChanged:  { //显示的文本本身的长度发生变化触发信号
114                     console.log("implicitWidth = ",text_test.implicitWidth)
115                     console.log("implicitHeight = ",text_test.implicitHeight)
116                     }
117     }
118 }

效果图:

原文地址:https://www.cnblogs.com/wxmwanggood/p/10929191.html