How to handle QString as file location and get its directory

I am compiling a list of files in a QStringList from the Qt GUI. Each of these files is a file .txtwith a corresponding video file in same_folder_as_txt/videos/.

Is there an easy way to manipulate QString objects as file paths? For example, given C:/some/path/foo.txtI want to getC:/some/path/videos/foo.avi

+5
source share
2 answers

You can convert them each to QDir, perform your modifications as a path, and then use absolutePath()to return QString.

+6
source

Given your way as QString s

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("/")
+8
source

All Articles