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

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

Related

'operator=' is deprecated: Use QDir::setPath() instead

Simple program that opens a GUI, you click one button to set curDir, click another button to set savDir, and a third button does some C++ code similar to
ls -l curDir > savDir.txt
One of my Qt functions:
void dirList::on_savBut_clicked(){
savDir = QFileDialog::getExistingDirectory(
this,"Save Location",QDir::homePath());
savPath = savDir.absolutePath();
ui->savText->setText(savPath);
}
On the savDir = QFileDialog::getExistingDirectory(... line I get a warning:
'operator=' is depreciated: Use QDir::setPath() instead
Could anyone give an example how I might incorporate setPath()?
You can simply write
savPath = QFileDialog::getExistingDirectory(
this,"Save Location",QDir::homePath());
Without using savDir.
I believe setPath is just a drop in replacement instead of using assignment for when updating a QDir with a QString path.
savDir = QFileDialog::getExistingDirectory(
this,"Save Location",QDir::homePath());
simply becomes
savDir.setPath(QFileDialog::getExistingDirectory(
this,"Save Location",QDir::homePath()));

QUrl doesn't work with parts from text file (Qt c++)

I want to make an url with some parts extracted from a text file.
I have the following code:
crack_order = new QFile("orders.txt"); //I have QFile *crack_order in headers
crack_order->open(QIODevice::ReadOnly);
while(!crack_order->atEnd()){
QStringList orders;
orders = QString(crack_order->readAll()).split('\n',QString::SkipEmptyParts);
foreach (QString order, orders){
ui->debugtextbox->setText(order); //until here, it works fine!
url = "http://www.example.com/customers/orders/"+order+"/another/thing.txt";
it does not work, the url request doesn't performed correctly. HOWEVER, if I put the following variable:
"QString order = "12233";
before "url", it works!
Then, I guess the problem is that it's getting the "order" from a text file, and QUrl doesn't like it.
Any idea?
Thanks in advanced.

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 get a complete path from one with wildcards?

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.

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.