欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Qt5】 Qt 中对于文件全名、基本名、路径、扩展名的获取

程序员文章站 2024-02-19 11:27:58
...

一、简介:

在Qt 中有时需要从文件名串中分别获取路径、文件全名、扩展名,这时就需要使用  QFileInfo。

二、示例代码:

#include <QFileInfo>
...

    QString filename = QFileDialog::getOpenFileName(this, QStringLiteral("打开文件"), "",
                                                    QStringLiteral("图像文件(*.jpg;*.jpeg;*.bmp;*.png);;所有文件(*.*)"));
    if (filename.isEmpty())  return 1;

    QFileInfo fileInfo(filename);
    qDebug() << QStringLiteral("文件所在路径: ") << fileInfo.path();
    qDebug() << QStringLiteral("文件及路径: ") << fileInfo.filePath();
    qDebug() << QStringLiteral("文件全名: ") << fileInfo.fileName();
    qDebug() << QStringLiteral("文件基本名: ") << fileInfo.baseName();
    qDebug() << QStringLiteral("扩展名: ") << fileInfo.suffix();

三、QFileInfo 的定义:

在 QFileInfo类中,还可以取得有关文件的更多信息。(类定义在 qfileinfo.h 头文件中)

class Q_CORE_EXPORT QFileInfo
{
    friend class QDirIteratorPrivate;
public:
    explicit QFileInfo(QFileInfoPrivate *d);

    QFileInfo();
    QFileInfo(const QString &file);
    QFileInfo(const QFile &file);
    QFileInfo(const QDir &dir, const QString &file);
    QFileInfo(const QFileInfo &fileinfo);
    ~QFileInfo();

    QFileInfo &operator=(const QFileInfo &fileinfo);
#ifdef Q_COMPILER_RVALUE_REFS
    QFileInfo &operator=(QFileInfo &&other) Q_DECL_NOTHROW { swap(other); return *this; }
#endif

    void swap(QFileInfo &other) Q_DECL_NOTHROW
    { qSwap(d_ptr, other.d_ptr); }

    bool operator==(const QFileInfo &fileinfo) const;
    inline bool operator!=(const QFileInfo &fileinfo) const { return !(operator==(fileinfo)); }

    void setFile(const QString &file);
    void setFile(const QFile &file);
    void setFile(const QDir &dir, const QString &file);
    bool exists() const;
    static bool exists(const QString &file);
    void refresh();

    QString filePath() const;
    QString absoluteFilePath() const;
    QString canonicalFilePath() const;
    QString fileName() const;
    QString baseName() const;
    QString completeBaseName() const;
    QString suffix() const;
    QString bundleName() const;
    QString completeSuffix() const;

    QString path() const;
    QString absolutePath() const;
    QString canonicalPath() const;
    QDir dir() const;
    QDir absoluteDir() const;

    bool isReadable() const;
    bool isWritable() const;
    bool isExecutable() const;
    bool isHidden() const;
    bool isNativePath() const;

    bool isRelative() const;
    inline bool isAbsolute() const { return !isRelative(); }
    bool makeAbsolute();

    bool isFile() const;
    bool isDir() const;
    bool isSymLink() const;
    bool isRoot() const;
    bool isBundle() const;

    QString readLink() const;
    inline QString symLinkTarget() const { return readLink(); }

    QString owner() const;
    uint ownerId() const;
    QString group() const;
    uint groupId() const;

    bool permission(QFile::Permissions permissions) const;
    QFile::Permissions permissions() const;

    qint64 size() const;

    // ### Qt6: inline these functions
#if QT_DEPRECATED_SINCE(5, 10)
    QT_DEPRECATED_X("Use either birthTime() or metadataChangeTime()")
    QDateTime created() const;
#endif
    QDateTime birthTime() const;
    QDateTime metadataChangeTime() const;
    QDateTime lastModified() const;
    QDateTime lastRead() const;
    QDateTime fileTime(QFile::FileTime time) const;

    bool caching() const;
    void setCaching(bool on);

protected:
    QSharedDataPointer<QFileInfoPrivate> d_ptr;

private:
    QFileInfoPrivate* d_func();
    inline const QFileInfoPrivate* d_func() const
    {
        return d_ptr.constData();
    }
};