C++ 如何在Linux中使用QT(QDir)创建目录?

C++ 如何在Linux中使用QT(QDir)创建目录?,c++,linux,qt,C++,Linux,Qt,我一直在尝试在Linux的根目录中创建一个目录。但由于我对Linux平台不太熟悉,我无法用QT编写正确的程序。你能看一下我的代码,告诉我哪里出错了吗 #include <QCoreApplication> #include <QDebug> #include <QDir> #include <QString> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv)

我一直在尝试在Linux的根目录中创建一个目录。但由于我对Linux平台不太熟悉,我无法用QT编写正确的程序。你能看一下我的代码,告诉我哪里出错了吗

#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QString>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
QDir mDir;
QString mpath="/home/qtfile";
if (!mDir.exists(mpath))
{
    mDir.mkpath(mpath);
    qDebug() <<"Created";
}
else if (mDir.exists(mpath))
{
    qDebug() <<"Already existed";
}
else
{
    qDebug()<<"Directory could not be created";
}
return a.exec();
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
QDir-mDir;
QString mpath=“/home/qtfile”;
如果(!mDir.存在(兆帕))
{
mDir.mkpath(mpath);

qDebug()这可能是@SamratLuitel在评论中提到的访问权限问题

因此,您可以尝试在适当的家中位置试一试,例如:

const QString& homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
QDir dir(homePath);
if (dir.mkdir("somedir"))
{
    //success
}

“/\home\qtfile”
可能应该是
“/home/qtfile”
?@Angew我想我试过了,我想你应该使用mDir.mkdir,而不是linux的mkpathRoot目录通常只限于根目录访问。最好是在你的主路径中创建一个目录(看起来像是你想要的操作).
QStandardPaths::writeablelocation(QStandardPaths::HomeLocation)
应提供主路径。只需附加所需目录并根据需要创建即可。
QDir-mydir(path);mydir.mkpath(desiredpath);
@SamratLuitel文档中提到的所有操作都有示例,请参考这些示例。