是否有像 "audio/*" 和 "video/*" 这样的所有音频和视频的 MIME 类型?

Is there such thing as a MIME type for all audio and video like "audio/*" and "video/*"?

我正在编写一个应用程序,通过拖放操作,我只想接受音频和视频类型。

这是 Qt5 中用于放置小部件的代码:

void DragDropFrame::dragEnterEvent(QDragEnterEvent* evt)
{
    if (frame_type == FRAME_TYPE::DROPPABLE)
    {
        if (evt->mimeData()->hasFormat("audio/*"))
        {
            evt->acceptProposedAction();
        }
        else
            evt->ignore();
    }
    else
        evt->ignore();
}

然而 "audio/*" 不起作用。该小部件不接受任何文件。我必须 "if-else" 所有可能的音频和视频 MIME 类型还是有其他更快的解决方案?

QMimeData::hasFormat does not process any kind of wildcards. It simply checks whether the mimetype you specify exists as-is in the list of supported formats (see the implementation).

您需要获取受支持的列表 formats() 并在其中搜索以 audio/video/.

开头的任何字符串

不,没有那样的通用 MIME 类型。

evt 会告诉您它包含的特定 MIME 类型。您可以进行 substring/pattern 匹配以查看是否有任何类型匹配您要查找的内容,例如:

void DragDropFrame::dragEnterEvent(QDragEnterEvent* evt)
{
    if (frame_type == FRAME_TYPE::DROPPABLE)
    {
        QStringList formats = evt->mimeData()->formats();
        if (!formats.filter("audio/").empty() ||
            !formats.filter("video/").empty())
        {
            evt->acceptProposedAction();
            return;
        }
    }

    evt->ignore();
}

或者:

void DragDropFrame::dragEnterEvent(QDragEnterEvent* evt)
{
    if (frame_type == FRAME_TYPE::DROPPABLE)
    {
        QRegExp regex("\b(audio|video)/*", Qt::CaseInsensitive, QRegExp::Wildcard);
        if (!evt->mimeData()->formats().filter(regex).empty())
        {
            evt->acceptProposedAction();
            return;
        }
    }

    evt->ignore();
}