9.6 Qt目录中的一些标准目录
9.6 Qt目录中的一些标准目录
在操作系统中,有一些标准的目录,例如不论是Mac系统还是Windows系统亦或者是Linux系统,它们为了方便用户,都会先在系统中预设一些所谓的标准的目录,并且都有其显示的名称,当然这些目录肯定是有固定不变的路径的。必然Windows系统中的桌面,图片目录、下载目录、音乐目录、视频目录、文档目录等。实际上除了这些目录之外还有一些其他的不被我们察觉的一些特殊的标准目录。例如应用程序的安装目录,应用程序的Temp文件目录,应用程序的缓存目录以及应用程序的配置文件目录和应用程序的数据目录等。那么在Qt中也有一种专门获取上述标准目录的类,就是QStandardPaths类,我们可以使用里面的displayName()方法来获取这些目录在操作系统中对用户所现实的名字,以及通过使用其standardLocations()函数来根据具体对应的枚举的标准目录来获取这些目录的具体路径。注意,该类在实际的开发中非常常用!以下我们通过一个例子来说明这两个函数的基本使用。
案例:通过QStandardPaths来获取操作系统中的标准目录的显示名称以及所在的路径
(1)建立Qt的GUI应用程序,项目名称为StandardPathsDemo,类名也为StandardPathsDemo,基类选择继承自QWidget类,不需要创建ui文件。
(2)在standardpathsdemo.h中加入以下GUI部件的成员
#ifndef STANDARDPATHSDEMO_H
#define STANDARDPATHSDEMO_H
#include <QLabel>
#include <QDebug>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QStandardPaths>
class StandardPathsDemo : public QWidget
{
Q_OBJECT
public:
StandardPathsDemo(QWidget *parent = nullptr);
~StandardPathsDemo();
private slots:
void slotGetStandardPath();
private:
QLabel*m_desktopLabel;//桌面路径Label
QLabel*m_documentLabel;//文档路径Label
QLabel*m_fontsLabel;//字体路径Label
QLabel*m_applicationsLabel;//应用程序路径Label
QLabel*m_musicLabel;//音乐路径Label
QLabel*m_moviesLabel;//视频路径Label
QLabel*m_pictureLabel;//图片路径Label
QLabel*m_tempLabel;//temporary文件路径Label
QLabel*m_homeLabel;//用户主目录路径Label
QLabel*m_dataLabel;//应用程序数据路径Label
QLabel*m_cacheLabel;//应用程序缓存路径Label
QLabel*m_genericCacheLabel;//常规缓存路径Label
QLabel*m_genericDataLabel;//常规应用程序数据路径Label
QLabel*m_runtimeLabel;//运行时socket文件路径Label
QLabel*m_configLabel;//程序配置文件路径Label
QLabel*m_downloadLabel;//下载路径Label
QLabel*m_genericConfigLabel;//常规程序配置文件路径Label
QLabel*m_appDataLabel;//应用程序数据路径Label
QLabel*m_applocalDataLabel;//应用程序本地数据路径Label
QLabel*m_appConfigLabel;//应用程序配置文件路径Label
private:
QLineEdit*m_desktopLineEdit;//桌面路径
QLineEdit*m_documentLineEdit;//文档路径
QLineEdit*m_fontsLineEdit;//字体路径
QLineEdit*m_applicationsLineEdit;//应用程序路径
QLineEdit*m_musicLineEdit;//音乐路径
QLineEdit*m_moviesLineEdit;//视频路径
QLineEdit*m_pictureLineEdit;//图片路径
QLineEdit*m_tempLineEdit;//Temp文件路径
QLineEdit*m_homeLineEdit;//用户主目录路径
QLineEdit*m_dataLineEdit;//数据目录路径
QLineEdit*m_cacheLineEdit;//应用程序缓存目录路径
QLineEdit*m_genericCacheLineEdit;//常规缓存路径
QLineEdit*m_genericDataLineEdit;//常规数据路径
QLineEdit*m_runtimeLineEdit;//运行时socket文件路径
QLineEdit*m_configLineEdit;//程序配置文件路径
QLineEdit*m_downloadLineEdit;//下载路径
QLineEdit*m_genericConfigLineEdit;//常规配置文件路径
QLineEdit*m_appDataLineEdit;//应用程序数据路径
QLineEdit*m_applocalDataLineEdit;//应用程序数据路径
QLineEdit*m_appConfigLineEdit;//应用程序配置文件路径
private:
QPushButton*m_getStandardPathPushButton;//获取标准路径
private:
QGridLayout*m_mainLayout;//主布局
};
#endif // STANDARDPATHSDEMO_H
(3)在standardpathsdemo.cpp中实现这些成员
#include "standardpathsdemo.h"
StandardPathsDemo::StandardPathsDemo(QWidget *parent):QWidget(parent)
{
m_mainLayout = new QGridLayout(this);
m_desktopLabel = new QLabel("桌面路径:");
m_desktopLineEdit = new QLineEdit;
m_desktopLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_desktopLabel,0,0);
m_mainLayout->addWidget(m_desktopLineEdit,0,1);
m_documentLabel = new QLabel("文档目录路径:");
m_documentLineEdit = new QLineEdit;
m_documentLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_documentLabel,1,0);
m_mainLayout->addWidget(m_documentLineEdit,1,1);
m_fontsLabel = new QLabel("字体目录路径:");
m_fontsLineEdit = new QLineEdit;
m_fontsLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_fontsLabel,2,0);
m_mainLayout->addWidget(m_fontsLineEdit,2,1);
m_applicationsLabel = new QLabel("App路径:");
m_applicationsLineEdit = new QLineEdit;
m_applicationsLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_applicationsLabel,3,0);
m_mainLayout->addWidget(m_applicationsLineEdit,3,1);
m_musicLabel = new QLabel("音乐目录:");
m_musicLineEdit = new QLineEdit;
m_musicLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_musicLabel,4,0);
m_mainLayout->addWidget(m_musicLineEdit,4,1);
m_moviesLabel = new QLabel("视频目录:");
m_moviesLineEdit = new QLineEdit;
m_moviesLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_moviesLabel,5,0);
m_mainLayout->addWidget(m_moviesLineEdit,5,1);
m_pictureLabel = new QLabel("图片目录:");
m_pictureLineEdit = new QLineEdit;
m_pictureLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_pictureLabel,6,0);
m_mainLayout->addWidget(m_pictureLineEdit,6,1);
m_tempLabel = new QLabel("Temp目录");
m_tempLineEdit = new QLineEdit;
m_tempLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_tempLabel,7,0);
m_mainLayout->addWidget(m_tempLineEdit,7,1);
m_homeLabel = new QLabel("用户主目录:");
m_homeLineEdit = new QLineEdit;
m_homeLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_homeLabel,8,0);
m_mainLayout->addWidget(m_homeLineEdit,8,1);
m_dataLabel = new QLabel("Data目录:");
m_dataLineEdit = new QLineEdit;
m_dataLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_dataLabel,9,0);
m_mainLayout->addWidget(m_dataLineEdit,9,1);
m_cacheLabel = new QLabel("Cache目录:");
m_cacheLineEdit = new QLineEdit;
m_cacheLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_cacheLabel,0,2);
m_mainLayout->addWidget(m_cacheLineEdit,0,3);
m_genericCacheLabel = new QLabel("常规Cache目录:");
m_genericCacheLineEdit = new QLineEdit;
m_genericCacheLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_genericCacheLabel,1,2);
m_mainLayout->addWidget(m_genericCacheLineEdit,1,3);
m_genericDataLabel = new QLabel("常规Data目录:");
m_genericDataLineEdit = new QLineEdit;
m_genericDataLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_genericDataLabel,2,2);
m_mainLayout->addWidget(m_genericDataLineEdit,2,3);
m_runtimeLabel = new QLabel("运行时目录:");
m_runtimeLineEdit = new QLineEdit;
m_runtimeLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_runtimeLabel,3,2);
m_mainLayout->addWidget(m_runtimeLineEdit,3,3);
m_configLabel = new QLabel("配置文件目录:");
m_configLineEdit = new QLineEdit;
m_configLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_configLabel,4,2);
m_mainLayout->addWidget(m_configLineEdit,4,3);
m_downloadLabel = new QLabel("下载目录:");
m_downloadLineEdit = new QLineEdit;
m_downloadLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_downloadLabel,5,2);
m_mainLayout->addWidget(m_downloadLineEdit,5,3);
m_genericConfigLabel = new QLabel("常规配置文件目录:");
m_genericConfigLineEdit = new QLineEdit;
m_genericConfigLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_genericConfigLabel,6,2);
m_mainLayout->addWidget(m_genericConfigLineEdit,6,3);
m_appDataLabel = new QLabel("App数据目录");
m_appDataLineEdit = new QLineEdit;
m_appDataLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_appDataLabel,7,2);
m_mainLayout->addWidget(m_appDataLineEdit,7,3);
m_applocalDataLabel = new QLabel("App本地数据目录:");
m_applocalDataLineEdit = new QLineEdit;
m_applocalDataLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_applocalDataLabel,8,2);
m_mainLayout->addWidget(m_applocalDataLineEdit,8,3);
m_appConfigLabel = new QLabel("App配置文件目录:");
m_appConfigLineEdit = new QLineEdit;
m_appConfigLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_appConfigLabel,9,2);
m_mainLayout->addWidget(m_appConfigLineEdit,9,3);
m_getStandardPathPushButton = new QPushButton("获取系统标准目录");
m_mainLayout->addWidget(m_getStandardPathPushButton,10,0,1,4);
connect(this->m_getStandardPathPushButton,&QPushButton::clicked,this,&StandardPathsDemo::slotGetStandardPath);
}
StandardPathsDemo::~StandardPathsDemo()
{
}
void StandardPathsDemo::slotGetStandardPath()
{
//获取这些目录操作系统上的显示出来的名称
m_desktopLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::DesktopLocation));
m_documentLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::DocumentsLocation));
m_fontsLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::FontsLocation));
m_applicationsLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::ApplicationsLocation));
m_musicLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::MusicLocation));
m_moviesLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::MoviesLocation));
m_pictureLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::PicturesLocation));
m_tempLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::TempLocation));
m_homeLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::HomeLocation));
m_dataLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::DataLocation));
m_cacheLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::CacheLocation));
m_genericCacheLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::GenericCacheLocation));
m_genericDataLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::GenericDataLocation));
m_runtimeLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::RuntimeLocation));
m_configLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::ConfigLocation));
m_downloadLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::DownloadLocation));
m_genericConfigLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::GenericConfigLocation));
m_appDataLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::AppDataLocation));
m_applocalDataLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::AppLocalDataLocation));
m_appConfigLabel->setText(QStandardPaths::displayName(QStandardPaths::StandardLocation::AppConfigLocation));
//获取这些标准的目录所在的路径
m_desktopLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::DesktopLocation).at(0));
m_documentLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::DocumentsLocation).at(0));
m_fontsLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::FontsLocation).at(0));
m_applicationsLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::ApplicationsLocation).at(0));
m_musicLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::MusicLocation).at(0));
m_moviesLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::MoviesLocation).at(0));
m_pictureLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::PicturesLocation).at(0));
m_tempLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::TempLocation).at(0));
m_homeLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::HomeLocation).at(0));
m_dataLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::DataLocation).at(0));
m_cacheLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::CacheLocation).at(0));
m_genericCacheLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::GenericCacheLocation).at(0));
m_genericDataLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::GenericDataLocation).at(0));
m_runtimeLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::RuntimeLocation).at(0));
m_configLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::ConfigLocation).at(0));
m_downloadLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::DownloadLocation).at(0));
m_genericConfigLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::GenericConfigLocation).at(0));
m_appDataLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::AppDataLocation).at(0));
m_applocalDataLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::AppLocalDataLocation).at(0));
m_appConfigLineEdit->setText(QStandardPaths::standardLocations(QStandardPaths::StandardLocation::AppConfigLocation).at(0));
}
(3)运行程序

(4)对QStandardPaths类中常用的方法的总结
#include <QCoreApplication>
#include <QStandardPaths>
#include <QStringList>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication theApp(argc, argv);
//QStandardPaths的常用方法
//由于该类没有成员方法,几乎都是所谓的静态方法,这里就说明一下这些静态方法的具体的使用
//1.[static] QString QStandardPaths::displayName(QStandardPaths::StandardLocation type)
{
qDebug() << "----[static] QString QStandardPaths::displayName(QStandardPaths::StandardLocation type)----";
//该方法用于获取到标准目录的显示的名称(也就是用户所看到的标准目录的显示的名称)
qDebug() << QStandardPaths::displayName(QStandardPaths::StandardLocation::DesktopLocation);
qDebug() << QStandardPaths::displayName(QStandardPaths::StandardLocation::MusicLocation);
qDebug() << QStandardPaths::displayName(QStandardPaths::StandardLocation::MoviesLocation);
qDebug() << QStandardPaths::displayName(QStandardPaths::StandardLocation::DownloadLocation);
qDebug() << QStandardPaths::displayName(QStandardPaths::StandardLocation::PicturesLocation);
qDebug() << QStandardPaths::displayName(QStandardPaths::StandardLocation::DocumentsLocation);
qDebug() << "----END [static] QString QStandardPaths::displayName(QStandardPaths::StandardLocation type)----";
}
//2.[static] QString QStandardPaths::findExecutable(const QString &executableName, const QStringList &paths = QStringList())
{
qDebug() << "----[static] QString QStandardPaths::findExecutable(const QString &executableName, const QStringList &paths = QStringList())----";
//该函数用于在指定的目录中查找名为executableName的可执行文件,当然这里的目录是很多的目录(因此这里面是一个StringList)
QStringList dirs;
dirs << "/bin" << "/sbin";
QString executablePath = QStandardPaths::findExecutable(QString("ls"),dirs);
if(executablePath.isEmpty())
{
qDebug() << "Not Found!";
}
else
{
qDebug() << executablePath;
}
qDebug() << "----END [static] QString QStandardPaths::findExecutable(const QString &executableName, const QStringList &paths = QStringList())----";
}
//3.[static] QString QStandardPaths::locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)
{
qDebug() << "----[static] QString QStandardPaths::locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)----";
//该函数用于在指定的标准目录中查找文件名名为fileName的文件或文件夹(这个究竟是找文件还是文件夹,是取决于第三个参数),找到就返回文件(文件夹)的绝对路径,否则就返回empty字符串
//比如这里我要在桌面上找一个文件名叫list.cpp
QString filePath = QStandardPaths::locate(QStandardPaths::StandardLocation::DesktopLocation,"list.cpp",QStandardPaths::LocateFile);
if(filePath.isEmpty())
{
qDebug() << "Not Found File Named list.cpp!";
}
else
{
qDebug() << filePath;
}
//这里我又想在桌面上找一个文件夹名叫demo
QString pathName = QStandardPaths::locate(QStandardPaths::StandardLocation::DesktopLocation,"demo",QStandardPaths::LocateOption::LocateDirectory);
if(pathName.isEmpty())
{
qDebug() << "Not Found Dir Named demo!";
}
else
{
qDebug() << pathName;
}
qDebug() << "----END [static] QString QStandardPaths::locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)----";
}
//4.[static] QStringList QStandardPaths::locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)
{
qDebug() << "----[static] QStringList QStandardPaths::locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)----";
//这个函数相比于前面那个locate,它可以同时在标准目录去找多个文件名叫fileName的文件,以StringList返回
const QStringList&filePaths = QStandardPaths::locateAll(QStandardPaths::StandardLocation::DesktopLocation,"list.cpp",QStandardPaths::LocateOption::LocateFile);
if(filePaths.count() > 0)
{
qDebug() << filePaths;
}
else
{
qDebug() << "Not Font Any File Named list!";
}
qDebug() << "----END [static] QStringList QStandardPaths::locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = LocateFile)----";
}
//5.[static] void QStandardPaths::setTestModeEnabled(bool testMode)
{
qDebug() << "----[static] void QStandardPaths::setTestModeEnabled(bool testMode)----";
//这个函数是用于设置对标准目录的测试模式,这样一来它会更改写入的目录是测试目录。避免从用户的配置中进行写入
QStandardPaths::setTestModeEnabled(true);
//它只会对GenericDataLocation, DataLocation, ConfigLocation, GenericConfigLocation, AppConfigLocation, GenericCacheLocation, CacheLocation这些目录有效,其他的目录无效
qDebug() << "----END [static] void QStandardPaths::setTestModeEnabled(bool testMode)----";
}
//6.[static] QStringList QStandardPaths::standardLocations(QStandardPaths::StandardLocation type)
{
QStandardPaths::setTestModeEnabled(false);//读者可以试试吧这里改为true,然后观察下面的路径和改为false有什么不同,就能理解前面的测试目录的含义了
qDebug() << "----[static] QStringList QStandardPaths::standardLocations(QStandardPaths::StandardLocation type)----";
//该函数会根据标准目录来返回对应的标准目录的绝对路径
qDebug() << QStandardPaths::standardLocations(QStandardPaths::StandardLocation::DesktopLocation);
qDebug() << QStandardPaths::standardLocations(QStandardPaths::StandardLocation::MusicLocation);
qDebug() << QStandardPaths::standardLocations(QStandardPaths::StandardLocation::MoviesLocation);
qDebug() << QStandardPaths::standardLocations(QStandardPaths::StandardLocation::DocumentsLocation);
qDebug() << QStandardPaths::standardLocations(QStandardPaths::StandardLocation::DocumentsLocation);
qDebug() << QStandardPaths::standardLocations(QStandardPaths::StandardLocation::PicturesLocation);
qDebug() << "----END [static] QStringList QStandardPaths::standardLocations(QStandardPaths::StandardLocation type)----";
}
//7.[static] QString QStandardPaths::writableLocation(QStandardPaths::StandardLocation type)
{
qDebug() << "----[static] QString QStandardPaths::writableLocation(QStandardPaths::StandardLocation type)----";
//该函数会返回当尝试往指定的标准目录中写文件时,能否被写入,如果可以就返回写入的路径,否则返回空串
qDebug() << QStandardPaths::writableLocation(QStandardPaths::StandardLocation::DesktopLocation);
qDebug() << "----END [static] QString QStandardPaths::writableLocation(QStandardPaths::StandardLocation type)----";
}
return theApp.exec();
}
本节代码:https://files.cnblogs.com/files/blogs/792763/StandardPathsDemo.zip?t=1720281591&download=true
https://files.cnblogs.com/files/blogs/792763/QStandardPathsTest.zip?t=1720291101&download=true

浙公网安备 33010602011771号