在实现文件查询功能时,想要实现一个返回按键,点击后可以返回到上级文件目录。
从程序的角度,我是要从 ./a/b/c 变成 .a/b
因此,我需要把最后一个"/"后面的内容删掉
而函数 lastIndexof('/')就可以获取最后一个 / 的位置,返回Int
搭配上remove() 处理字符串,就可以获取上级文件目录的字符串了
以下是案例
void Book::on_m_pBackPB_clicked()
{
QString strCurPath=m_curPath;
QString strRootPath=client::getInstance().curPath();
if(strCurPath==strRootPath)
{
QMessageBox::warning(this,"返回","返回失败:已经在最开始的文件夹目录");
}
else//返回到上一个路径
{
//"./aa/bb" --> "./aa"
int index=strCurPath.lastIndexOf('/');
strCurPath.remove(index,strCurPath.size()-index);
m_curPath=strCurPath;
qDebug()<<"return --> "<<m_curPath;
}
}

516

被折叠的 条评论
为什么被折叠?



