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.
Option one is my preferred solution, however option two will do as well..
Following is a short code describing my problem:
#include <QApplication>
#include <QFileDialog>
#include <QStringList>
#include <phonon/backendcapabilities.h>
QStringList mime_to_exts(QString mime)
{
// WHAT TO REALLY DO ??
// NEEDLESS TO SAY; THIS IS WRONG...
return QStringList(mime.split("/").back().split('-').back());
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
app.setApplicationName("phononext");
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_exts(*i);
}
QString filter = QString("All Files开发者_如何学Python(*)");
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::getOpenFileName(NULL, "Open Audio File", QString(), filter);
}
You can filter on mime types with QFileDialog::setProxyModel
. You'll probably want to subclass QSortFilterProxyModel
and override filterAcceptsRow
so that it accepts only when the file is of the appropriate mime-type. Hopefully this, when bound with an easy way to identify a file's mime-types, will serve as a good solution.
Call availableMimeTypes()
on the Phonon backend and then loop through the resulting MIME-type list and for each one enumerate the extensions returned by QMimeType::extensions()
.
精彩评论