You are trying to mix the static getOpenFileName() convenience function, which expects to get the working directory in its calling parameters, and using the dialog exec() or show() methods.
Try either the static approach:
void MainWindow::on_pushButton_clicked()
{
tr("Open File"),
"/tmp",
tr("All Files (*.*)")
);
if ( filename.isEmpty() )
return;
}
void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"),
"/tmp",
tr("All Files (*.*)")
);
if ( filename.isEmpty() )
return;
}
To copy to clipboard, switch view to plain text mode
or the conventional dialog approach
void MainWindow::on_pushButton_clicked()
{
dialog.setDirectory( "/tmp" );
if (dialog.exec()) {
filename = filenames.at(0);
}
else
// dialog rejected
}
void MainWindow::on_pushButton_clicked()
{
QFileDialog dialog;
QString filename;
dialog.setDirectory( "/tmp" );
dialog.setFileMode(QFileDialog::ExistingFile);
if (dialog.exec()) {
QStringList filenames = dialog.selectedFiles();
filename = filenames.at(0);
}
else
// dialog rejected
}
To copy to clipboard, switch view to plain text mode
The behaviour is inconsistent if you mix the two it would seem.
Bookmarks