0

I am trying to make a simple widget which contains a lineedit which shows the file name and a button to open a filedialog. and now I want to check if the file-extension is valid, in this case, a image file ending with jpg, png or bmp. I solved this with QFileInfo and QList, this code is in my btn_clicked slot:

QString filename = QFileDialog::getOpenFileName(this, tr("Select an image File", "", tr("Image Files (*.bmp *.jpg *.png);; All Files(*)"));
QList<QString> ext_list;
ext_list<<"bmp"<<"jpg"<<"png";
QFileInfo fi(filename);
QString ext = fi.suffix();
if (ext_list.contains(ext)){
   // lineedit->setText(filename);
}
else {
QMessageBox msgBox;
msgBox.critical(0, "Error", "You must select a valid image file");

it works, but is there a more simple/elegant way to achieve the goal? Thx for your help.

yangsunny
  • 656
  • 5
  • 13
  • 32
  • 1
    Did you try to remove your `All Files(*)` from `QFileDialog` filter ? – AlexandreP May 06 '15 at 10:08
  • possible duplicate of [How to set selected filter on QFileDialog?](http://stackoverflow.com/questions/1604440/how-to-set-selected-filter-on-qfiledialog) – demonplus May 06 '15 at 11:25
  • @demonplus: thats not what I want here, he wants to select a certain file typ(JPEG) contains different file-extensions(*jpg,*jpeg) as default filter. – yangsunny May 06 '15 at 12:04
  • @AlexandreP: yes, that sure works to prevent someone to choose a different file type in the first place. but I am more interested in if theres any other way to access the filter directly without making an extra qlist. i.e. if I have tr("JPEG (*.jpg *.jpeg);; Bitmap (*.bmp *.dib);; GIF (*.gif)) and I want to show a MessageBox if the chosen file is none of it. I will read about the setNameFilters first and then edit my answer again. – yangsunny May 06 '15 at 12:05

1 Answers1

0

You might be interested by the setNameFilters function : http://doc.qt.io/qt-5/qfiledialog.html#setNameFilters

Update

If you want to filter images without naming each extensions, you should use QMimeDatabase. This will allow you to define your filter for the QFileDialog and then get a list of extensions to check.

For example with jpeg and png:

QStringList mimeTypeFilters;
mimeTypeFilters << "image/jpeg" << "image/png";

QFileDialog fd;
fd.setFileMode(QFileDialog::ExistingFile);
fd.setMimeTypeFilters(mimeTypeFilters);
fd.exec();

QString filename = fd.selectedFiles().count() == 1 ? fd.selectedFiles().at(0) : "";

QMimeDatabase mimedb;
if(!mimeTypeFilters.contains(mimedb.mimeTypeForFile(filename).name()))
{
    // something is wrong here !
}
AlexandreP
  • 410
  • 1
  • 5
  • 17
  • as far as I understand it, its just another way to set the filter which need a instance of QFileDialog. In my case I used the static methode getOpenfileName(),how can I use setNameFilters then? – yangsunny May 06 '15 at 12:27
  • one little question, with your solution with mimeTypeFilters set on these 2 image types, theres actually no way I can select a "wrong" type of files. So, is there a mimetype for "all files" for mimeTypeFilters so I can test the if-branch? – yangsunny May 06 '15 at 13:43
  • edit: fount it in the documentation for QFileDialog "application/octet-stream". thx for the answer. – yangsunny May 06 '15 at 13:48