一 、使用注意
QFileInfo::isDir()
是 Qt 中用于检查指定路径是否为目录的函数。这个函数会返回一个布尔值,表示路径是否指向一个存在的目录。
如果路径存在且指向一个目录,则 QFileInfo::isDir()
会返回 true
;如果路径不存在或者路径存在但不是一个目录,则会返回 false
。
因此,QFileInfo::isDir()
不仅仅是在路径存在时返回 true
,而是在路径存在且指向一个目录时返回 true
。如果路径不存在或者路径存在但不是一个目录,则会返回 false
。
二、过滤目录或者以exe结尾的文件
m_addedit.setToolTip("D:\\kk\\;aa.exe");
if (addstr.endsWith(".exe"))
{
if (addstr.contains('\\') || addstr.contains("/"))
{
CMessageBox::showError(NULL, tr("Error"), tr("Invalid directory or file!"), QMessageBox::Ok /*| QMessageBox::Cancel*/, QMessageBox::No);
return;
}
}
else
{
if (!addstr.contains("\\") || !addstr.endsWith("\\") || !QFileInfo(addstr).isDir())
{
CMessageBox::showError(NULL, tr("Error"), tr("Invalid directory or file!"), QMessageBox::Ok /*| QMessageBox::Cancel*/, QMessageBox::No);
return;
}
}