QFileDialog 来自 mime 类型的过滤器
我希望 QFileDialog 中的过滤器能够匹配相关平台上 Phonon 支持的所有音频文件类型。
1 - 但是我无法在 Qt 中找到在过滤器中使用 mime 类型的方法。我怎样才能做到这一点?
2 - 或者如何手动找到 mimetypes 相应的文件扩展名?该解决方案应该基于 Qt,或者至少是跨平台的,并且在 Qt 所在的任何地方都受支持。
选项一是我的首选解决方案,但是选项二也可以。
以下是描述我的问题的简短代码:
#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(*)");
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);
}
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(*)");
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
QFileDialog::setProxyModel
。您可能想要子类QSortFilterProxyModel
并覆盖 < a href="http://doc.trolltech.com/4.6/qsortfilterproxymodel.html#filterAcceptsRow" rel="nofollow noreferrer">filterAcceptsRow
,以便仅当文件为适当的哑剧类型。希望当与识别文件的 mime 类型的简单方法结合时,这将成为一个很好的解决方案。You can filter on mime types with
QFileDialog::setProxyModel
. You'll probably want to subclassQSortFilterProxyModel
and overridefilterAcceptsRow
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.在 Phonon 后端调用
availableMimeTypes()
,然后循环遍历生成的 MIME 类型列表,并为每个列表枚举QMimeType::extensions()
返回的扩展。Call
availableMimeTypes()
on the Phonon backend and then loop through the resulting MIME-type list and for each one enumerate the extensions returned byQMimeType::extensions()
.