How to setNameFilters for QFileDialog?
-
I have some code:
@
void MainWindow::on_actionOpen_triggered()
{
QStringList filters;
filters << "SH3 Log files (*.clg .cfg)"
<< "Text files (.txt)";
QString file = QFileDialog::getOpenFileName(this,"Open a file");;if(!file.isEmpty()) { QFile sFile(file); if(sFile.open(QFile::ReadOnly | QFile::Text)) { mFilename = file; QTextStream in(&sFile); QString text = in.readAll(); sFile.close(); ui->textEdit->setPlainText(text); } }
}
@I'd like to add setNameFilter(filters); in the QFileDialog area but don't know hot to implement it.
Thanks.
-
There is a complete example how to do this, or do you have some problems using it?
http://qt-project.org/doc/qt-4.8/qfiledialog.html#setNameFilters
Edit: If you want to use the static method there is also an example here:
http://qt-project.org/doc/qt-4.8/qfiledialog.html#getOpenFileName -
I figured it out. I had to make an instance os QFileDialog. I called it dialog.
-
Well done. Please mark your threads as solved if so.
-
Note it is also possible with the static methods (native file dialog) like this:
@
QString filter = "SH3 Log files (*.clg .cfg);;Text files (.txt)";
QString file = QFileDialog::getOpenFileName(this,"Open a file", "", filter);
@
Note the ";;" in the filter string -
I actually ended up doing this:
@
//Open dialog with filepath and filename filters.
QString file = dialog.getOpenFileName(this,tr("Open a file"), filepath, tr("SH3 Log files (*.clg *.cfg *.txt)"));
@