Reputation:
I wanted to open QFileDialog with a specific path like: //Machine/C$/Users/. I have implemented the following function but it doesn't work.
void DownloadFM::on_pushButtonSource_clicked()
{
QFileDialog o_dialogSource;
o_dialogSource.setDirectory(absolutePath);
QString fileName = QFileDialog::getOpenFileName(this, "Choose File");
if(fileName.isEmpty())
return;
ui->lineEditSource->setText(fileName);
}
Upvotes: 0
Views: 205
Reputation: 19140
This code
QFileDialog o_dialogSource;
o_dialogSource.setDirectory(absolutePath);
and this
QString fileName = QFileDialog::getOpenFileName(this, "Choose File");
are completely independent. The former creates a local dialog object, sets the path in it, and... never shows the dialog. The latter creates another dialog—that does appear on the screen—passing the default value of the third argument as const QString &dir=QString()
(see this function documentation), thus not setting the path you wanted.
The proper way is to remove the useless o_dialogSource
lines, and then add the necessary argument to the getOpenFileName
call:
QString fileName = QFileDialog::getOpenFileName(this, "Choose File", absolutePath);
Upvotes: 0
Reputation: 550
For example, if you want to open a dialog at desktop location do as follows:
QString fileName = QFileDialog::getOpenFileName(this, "Choose File",QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
Please notice that you have to #include <QStandardPaths>
Upvotes: 1