QT常用函数

一、此工程实现获取当前工程目录、获取指定目录下的所有文件名、获取系统当前时间、获取自1970.1.1日以来到当前时间的毫秒数、打开选择目录的对话框、打开选择目录的对话框、打开选择文件路径的对

二、代码实现如下:

//1、获取Windows下的各种系统路径,其中MARK是我的电脑名
/*
QStandardPaths::DesktopLocation,         "C:/Users/MARK/Desktop"
QStandardPaths::DocumentsLocation,       "C:/Users/MARK/Documents"
QStandardPaths::FontsLocation,           "C:/windows/Fonts"
QStandardPaths::ApplicationsLocation,    "C:/Users/MARK/AppData/Roaming/Microsoft/Windows/Start Menu/Programs"
QStandardPaths::MusicLocation,           "C:/Users/MARK/Music"
QStandardPaths::MoviesLocation,          "C:/Users/MARK/Videos"
QStandardPaths::PicturesLocation,        "C:/Users/MARK/Pictures"
QStandardPaths::TempLocation,            "C:/Users/MARK/AppData/Local/Temp"
QStandardPaths::HomeLocation,            "C:/Users/MARK"
QStandardPaths::DataLocation,            "C:/Users/MARK/AppData/Local/test6"
QStandardPaths::CacheLocation,           "C:/Users/MARK/AppData/Local/test6/cache"
QStandardPaths::GenericDataLocation,     "C:/Users/MARK/AppData/Local"
QStandardPaths::RuntimeLocation,         "C:/Users/MARK"
QStandardPaths::ConfigLocation,          "C:/Users/MARK/AppData/Local/test6"
QStandardPaths::DownloadLocation,        "C:/Users/MARK/Downloads"
QStandardPaths::GenericCacheLocation,    "C:/Users/MARK/AppData/Local/cache"
QStandardPaths::GenericConfigLocation,   "C:/Users/MARK/AppData/Local"
QStandardPaths::AppDataLocation,         "C:/Users/MARK/AppData/Roaming/test6"
QStandardPaths::AppConfigLocation,       "C:/Users/MARK/AppData/Local/test6"
QStandardPaths::AppLocalDataLocation
*/
QString MainWindow::getPath()
{
    QString sPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);

    return sPath;
}

//2、获取当前工程目录
QString MainWindow::getCurrentPath()
{
    return QDir::currentPath();
}

//3、获取指定目录下的所有文件名
QStringList MainWindow::getFileNames(QString sPath)
{
    if (sPath.isEmpty()) return QStringList();

    //获取指定路径下的所有图片名称
    QDir dir(sPath);
    QStringList lisFilters;
    lisFilters << "*.bmp" << "*.jpg" << "*.jpeg" << "*.png" << "*.gif" << "*.tiff" << "*.*";
    QStringList listImgFileNames = dir.entryList(lisFilters, QDir::Files | QDir::Readable, QDir::Name);

    return listImgFileNames;
}

//4、获取系统当前时间
//iType==0:获取 星期 月份 日 时:分:秒 年 (周一 五月 25 15:08:21 2020)
//iType==1:获取年月日时分秒毫秒          (2020-05-25 15:09:38.906)
//iType==2:获取年月日时分秒              (2020-05-25 15:09:38)
//iType==3:获取年月日                    (2020-05-25)
//iType==4:获取时分秒                    (15:09:38)
QString MainWindow::getCurrentTime(int iType)
{
    QString sNow;

    if (iType == 0)      sNow = QDateTime::currentDateTime().toString();
    else if (iType == 1) sNow = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    else if (iType == 2) sNow = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    else if (iType == 3) sNow = QDateTime::currentDateTime().toString("yyyy-MM-dd");
    else if (iType == 4) sNow = QDateTime::currentDateTime().toString("hh:mm:ss");

    return sNow;
}

//5、获取自1970.1.1日以来到当前时间的毫秒数
QString MainWindow::getMSecsSinceEpoch()
{
    QDateTime dt = QDateTime::currentDateTime();
    qint64 ms = dt.toMSecsSinceEpoch();

    return QString::number(ms, 10);
}

//6、打开选择目录的对话框
QString MainWindow::selectPathDlg()
{

    QString sDesktop = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString sPath = QFileDialog::getExistingDirectory(this, "选择目录", sDesktop, QFileDialog::ShowDirsOnly);

    return sPath;
}

//7、打开选择文件路径的对话框
QString MainWindow::selectFileNameDlg()
{
    QString sDesktop = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString sPath = QFileDialog::getOpenFileName(this, "请选择文件", sDesktop, "*.txt \r\n *.xls \r\n *.bmp \r\n *.*");

    return sPath;
}

//8、打开选择保存文件对话框
QString MainWindow::saveFileNameDlg()
{
    QString sDesktop = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString sPath = QFileDialog::getSaveFileName(this, "选择保存文件名", sDesktop, "*.txt \r\n *.xls");

    return sPath;
}

//9、创建目录
//如果目录不存在则创建目录
//如果目录存在则创建失败
bool MainWindow::makeDir(QString sPath)
{
    QDir dir;
    return dir.mkdir(sPath);
}

//10、删除目录
//如果目录存在则删除成功
//如果目录不存在则删除失败
bool MainWindow::removeDir(QString sPath)
{
    QDir dir;
    return dir.rmdir(sPath);
}

//11、递归一次性创建多层目录
//如果目录存在则创建失败
//如果目录不存在则创建成功
bool MainWindow::makePath(QString sPath)
{
    QDir dir;
    return dir.mkpath(sPath);
}

//12、递归一次性删除多层目录
//如果目录存在则创建失败
//如果目录不存在则创建成功
bool MainWindow::removePath(QString sPath)
{
    QDir dir;
    return dir.rmpath(sPath);
}

//13、删除文件
//如果文件存在则删除成功
//如果文件不存在则删除失败
bool MainWindow::removeFile(QString sFilePath)
{
    QDir dir;
    return dir.remove(sFilePath);
}

//14、重命名目录/文件
//如果目录/文件存在则重命名成功
//如果目录/文件不存在则重命名失败
bool MainWindow::reMakeName(QString sOldName, QString sNewName)
{
    QDir dir;
    return dir.rename(sOldName, sNewName);
}

//15、判断目录/文件是否存在
//如果目录/文件存在则返回true
//如果目录/文件不存在则返回false
bool MainWindow::isExists(QString sName)
{
    QDir dir;
    return dir.exists(sName);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark-puls

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值