2016-11-29 617 views
-1

我一直在試圖在Linux的根目錄下創建一個目錄。但由於我對Linux平臺不太熟悉,我無法在QT中編寫正確的程序。你能看看我的代碼,並告訴我我哪裏錯了嗎?如何在Linux中使用QT(QDir)創建目錄?

#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(); 
} 

謝謝您的時間和考慮

編輯: - 謝謝大家。現在解決了這個問題

+0

是'「/ \家\ qtfile」'也許應該是'「/家/ qtfile」 '? – Angew

+0

@Angew我想我試過 –

+0

我想你應該使用mDir.mkdir而不是mkpath – Dusteh

回答

0

這可能是@SamratLuitel在評論中寫的訪問權限問題。

因此,你可以嘗試給它在適當的歸屬位置一展身手,例如:

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