QFileDialog filter from mime-types

Posted by Mathias on Stack Overflow See other posts from Stack Overflow or by Mathias
Published on 2010-04-08T12:40:11Z Indexed on 2010/04/08 12:43 UTC
Read the original article Hit count: 628

I want the filter in a QFileDialog to match all audio file types supported by Phonon on the platform in question.

1 - However I am not able to find a way in Qt to use mime types in a filter. How can I do that?

2 - Or how can I find the corresponding file extensions for the mimetypes manually? The solution should be Qt based, or at least be cross platform and supported everywhere Qt is.

Following is a short code describing my problem:

#include <QApplication>
#include <QFileDialog>
#include <QStringList>
#include <phonon/backendcapabilities.h>

QString mime_to_ext(QString mime)
{
   // WHAT TO REALLY DO ??
   // NEEDLESS TO SAY; THIS IS WRONG...
   return mime.split("/").back().split('-').back();
}

int main(int argc, char **argv)
{
   QApplication app(argc, argv);

   QStringList p_audio_exts;
   QStringList p_mime_types = Phonon::BackendCapabilities::availableMimeTypes();
   for(QStringList::iterator i = p_mime_types.begin(), ie = p_mime_types.end(); i != ie; i++)
   {
      if((*i).startsWith("audio"))
         p_audio_exts << mime_to_ext(*i);
   }


   QString filter = QString("All Files(*)");
   if(!p_audio_exts.isEmpty())
   {
      QString p_audio_filter = QString("Audio Files (*.%1)").arg(p_audio_exts.join(" *."));
      filter = QString("%1;;%2").arg(p_audio_filter).arg(filter);
   }

   QFileDialog dialog(NULL, "Open Audio File", QString::null, filter);
   dialog.exec();
}

© Stack Overflow or respective owner

Related posts about qt

Related posts about mime-type