2012-09-06 47 views
5

我從Qt GUI中將文件列表收集到QStringList中。這些文件中的每一個都是.txt文件,在same_folder_as_txt/videos/中有相應的視頻文件。如何將QString作爲文件位置並獲取其目錄

有沒有一種簡單的方法來操縱QString對象作爲文件路徑?例如,給定C:/some/path/foo.txt,我想找回C:/some/path/videos/foo.avi

回答

6

您可以將它們各自轉換爲QDir,執行您的修改路徑,然後用absolutePath()得到QString回來。

8

使用QStringList中構建的路徑鑑於你的路徑作爲QStrings

info = QFileInfo(s) 
// Get the name of the file without the extension 
base_name = info.baseName() 
// Add a ".avi" extension 
video_file = QStringList((base_name, "avi")).join(".") 
// Get the directory 
dir_name = info.path() 
// Construct the path to the video file 
video_path = QStringList((dir_name, QString("videos"), video_file).join("/") 
+1

就像使用光劍打開一個金槍魚可以一點點。爲什麼不使用連接運算符? video_file = base_name%QLatin1String(「.avi」; and video_path = dir_name%QDir :: separator()%QLatin1String(「videos」)%QDir :: separator()%video_file; –

相關問題