How to get a complete path from one with wildcards? - c++

I have a path like:
C:\path\to\my*file\
and I would like to get the corresponding full path (if it exists):
C:\path\to\my1file\
I tried with this Qt code, but the result is the same path I had at the beginning:
QStringList filters;
filters << "C:/path/to/my*file/";
QDir dir;
dir.setNameFilters(filters);
QStringList dirs = dir.entryList(filters);
_path = dirs.at(0); // get the first path only
Shouldn't I get all the files/directories that get through the filter?
Why is _path equal to "C:/path/to/my*file/"?
Is it possible to do the same thing with C++98/STL only? (In this project I cannot use Boost/C++11).

Use filters to filter files/folders, and set the path in QDir object:
QStringList filters;
filters << "my*file";
QDir dir("C:/path/to/");
QStringList dirs = dir.entryList(filters);
if (dirs.size() > 0)
{
qDebug() << dirs.at(0);
}

Expanding file names is called globbing. On Windows the functions FindFirstFile() / FindNextFile() do globbing.

Related

QFileDialog in c++: "no matching function for call"

I want to let the user choose a folder so I can display and sort its contents somewhere else. The best way to do this seems to be using QFileDialog. Here's a snippet of the code I'm using:
> #include <QFileDialog>
.....
void someEvent(){
QString path = QFileDialog::getExistingDirectoryUrl(this, tr("Choose a Folder"), QDir::home());
}
When I try to compile this I get the error:
no matching function for call to QFileDialog::getExistingDirectoryUrl(MainWindow*, QString, QDir) path = QFileDialog::getExistingDirectoryUrl(this, tr("Choose a Folder"), QDir::home());
Note: I'm running Fedora 25 on this PC and I'm wondering whether that might be the issue?
You have 2 choices depending on your needs, the first one being the best :
getExistingDirectory :
QString path = QFileDialog::getExistingDirectory(this,tr("Choose a Folder"),QDir::homePath());
getExistingDirectoryUrl :
QUrl url = QFileDialog::getExistingDirectoryUrl(this,tr("Choose a Folder"),QUrl(QDir::homePath()));
QString path = url.toString();

Rename files with same name but differing extensions

If a tickbox is clicked on my application, all files in a specified folder having the same pre-dot name, (eg. TESTCRC32.xxx) will be re-named. If the filename is something else, (eg. Pic.jpg) this file will not be renamed.
How can I go about this? I was thinking a for loop...
void SecondDlg::OnTickBox()
{
// Add code here...
CString oldFile = myPath.Left(myPath.ReverseFind(_T('.')));
rename(oldFile, newFile);
}
You are doing wrong. lets take an example, Suppose myPath is having path "C:\abc\xyz.bmp"
After this line:
CString oldFile = myPath.Left(myPath.ReverseFind(_T('.')));
Now:
oldFile = "C:\\abc\\xyz"; // extension removed
At last you are calling rename
rename(oldFile, newFile); //you can use myPath instead of oldFile
As oldFile = "C:\abc\xyz"; and which is not the correct path, so it is not renamimg the file.
you should pass full path of the file(C:\abc\xyz.bmp).

Setting the name of a text file to a variable in qt

I'm exporting data to a text file in qt every time a run a code. With my current code that file is overwritten each time. My question is how can I set the title to be a variable eg pulse_freq, this way new files will be created based on my variable values. I just can't get the syntax right.
Is there a way to put my files in a folder in the same directory as my build files? I need my code to be cross platform and if I use the full path name it's apparently incompatible with any non-windows OS. If I just name the files there'd be too much clutter in the folder. Relevant code is below:
// Export to data file
QString newname = QString::number(variables.nr_pulses);
QString filename = "C:/Users/BIC User/Documents/BIC Placement Documents/QT_data/Data.txt";
QFile file( filename );
You can just use something along lines:
QString s1 = "something";
QString s2 = " else";
QString s3 = s1 + s2;
QString concatenation with overloaded operator+ works like charm.
And about referencing folder you're in, instead of hardcoding its path, use QDir::currentPath()
Thus, your filename creation should look like the following:
QString folder = QDir::currentPath();
QString file = QString::number(variables.nr_pulses); //or whatever else you want it to be
QString extension = ".txt" // or whatever extension you want it to be
QString full_filename = folder + file + extension;
In order not to mess with appending string after the extension, just separate it into another QString and concatenate those 3 elements as above (folder + file + extension).

How to treat a QString as a file location and get its directory

I gather a list of files into a QStringList from a Qt GUI. Each of these files is a .txt file, with 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.txt , I want to retrieve C:/some/path/videos/foo.avi
Given your path as a 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("/")
You can convert them each to QDir, perform your modifications as a path, and then use absolutePath() to get the QString back.

Filter QFileInfoList files with Qt

I have a QFileInfoList (list) that contains info about a directory and its file
QFileInfoList list = directory.entryInfoList();
How can I apply filters to remove everything except image file(jpg, gif, png etc.) ?
Here is a simple foreach loop that only removes everything that is not a file
foreach (QFileInfo f, list){
if (!f.isFile()){
list.removeOne(f);
}
How can I apply filters to remove everything except image file(jpg, gif, png etc.) ?
QDir::entryInfoList() takes name filters, if you're comfortable determining images by extension:
QStringList nameFilter;
nameFilter << "*.png" << "*.jpg" << "*.gif";
QFileInfoList list = directory.entryInfoList( nameFilter, QDir::Files );
First of all, why won't you use proper flags to QDir::entryInfoList and filter out everything that is not a directory, and desired file extensions.
Secondly, you may use on each file `bool QImageReader::canRead(, but it will access files to check if they're images, so it will be quite slow.
Also you may try to build up supported extensions with QImageReader::supportedImageFormats (), and add them as filter to entryInfoList