QML对地址的操作——选择,获取文件名称,裁剪,

QML对地址的操作——选择,获取,裁剪

QML获取路径有多种方式这里详细介绍FileDialog
部分代码展示

    property int fileleng: 0
    property int filelength: 0
    property string plannames: null
    property int nameLength: 0
    //上面四个是定义的变量,用于保存
   FileDialog {
          id: fileDialog
          title: "Please choose a file"
          folder: "../../image/"
          visible: false
          selectFolder:false
          onAccepted: {
              console.log("You chose: " + fileDialog.fileUrls)
              fileleng = fileDialog.fileUrl.toString().length - 8;
              filelength = fileDialog.fileUrl.toString().length - fileDialog.folder.toString().length-1
             console.log("你的选择+"+fileDialog.fileUrl.toString().substring(fileDialog.folder.toString().length+1))
              planimport.plannames = fileDialog.fileUrl.toString().substring(fileDialog.folder.toString().length+1);
              container.text = planimport.plannames;
              nameLength =planimport.plannames.toString().length;
              fileDialog.close();
     }

fileUrl是获得你选择的文件路径的URL
如:file:///C:\Qt\Qt5.12.1\Tools\QtCreator QtCreator是你选取的文件夹,那么fileUrls获取的路径就是file:///C:\Qt\Qt5.12.1\Tools\QtCreator。
folder是你选择文件的URL
如:folder获取的路径就为file:///C:\Qt\Qt5.12.1\Tools
tostring() 将地址改变成字符串
length 获取字符串长度
substring()对字符串进行裁剪,括号中含有两个参数,当只写一个参数,代表裁剪的位置,当写了两个参数时,前者是裁剪的位置,后者是数量,

//截取指定长度的字符串
console.log(IDName.substring(0,Math.min(11,IDName.length))) //最好使用这种方式,

这段代码是为了获取选中文件的名字,即上述的QtCreator文件夹的名称。
在QML中获取的文件路径通常带有file:///,当你要传递绝对路径时,要将file:///去掉,如果在C++中处理,只需要mid(),可以参照上篇文章

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值