gpt4 book ai didi

c++ - 如何在 Mac OS X 10.5/10.6 上的 Qt 4.4 中接收来自 Apple 地址簿的拖放

转载 作者:太空狗 更新时间:2023-10-29 22:53:37 24 4
gpt4 key购买 nike

我正在尝试将标准 Apple 地址簿应用程序中的拖放事件捕获到我的 Qt 应用程序。此代码适用于 Qt 4.4。在 Mac OS X 10.4 上:

void 
MyView::contentsDropEvent( QDropEvent* e )
{
QList<QUrl> urls = e->mimeData()->urls();
...

然后我可以使用该 URL 获取 vCard。太棒了。

但是从 Mac OS X 10.5 开始,苹果地址簿似乎不再支持 text/uri-list。所以 e->mimeData()->urls() 返回一个空列表。更糟糕的是,e->mimeData()->formats() 返回一个空列表。我如何找出他们拖动了哪些 vCard?

这是诺基亚 Qt 工程师对这个问题的评论:

"Adressbook stopped providing drop data as text/uri-list compatible flavor data in OS 10.5. Not much we can do about that. The flavor they provide instead is 'public.vcard'. We could put up support for this as an implementation request, but my gut feeling is that this is too application specific, and can just as well be implemented by the app developer by subclassing QMacMimeData"

但是 Qt 4.4 或 4.5 文档中没有 QMacMimeData。关于如何找出他们拖拽的内容有什么想法吗?

最佳答案

Qt/Nokia 的 richardmg 好心地为我提供了一些示例代码。我已经填补了一些空白。这现在在 Mac OS X 10.5 上工作正常。

#include <QtGui>

class VCardMime : public QMacPasteboardMime
{
public:
VCardMime() : QMacPasteboardMime(MIME_ALL)
{ }

QString convertorName()
{
return QString("VCardMime");
}

bool canConvert(const QString &mime, QString flav)
{
return mimeFor(flav) == mime;
}

QString mimeFor(QString flav)
{
if (flav == QString("public.vcard"))
return QString("application/x-mycompany-VCard");
return QString();
}

QString flavorFor(const QString &mime)
{
if (mime == QString("application/x-mycompany-VCard"))
return QString("public.vcard");
return QString();
}

QVariant convertToMime(const QString &mime, QList<QByteArray> data, QString flav)
{
QByteArray all;
foreach ( QByteArray i, data )
{
all += i;
}
return QVariant( all );
}

QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav)
{
// Todo: implement!
return QList<QByteArray>();
}

};

class TestWidget : public QWidget
{

public:
TestWidget() : QWidget(0)
{
new VCardMime();
setAcceptDrops(true);
}

void contentsDropEvent(QDropEvent* e)
{
if ( e->mimeData()->hasFormat( "application/x-mycompany-VCard" ) )
{
QString s = QString( e->mimeData()->data( "application/x-mycompany-VCard" ) );

// s now contains text of vcard

e->acceptProposedAction();
}
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
TestWidget wid1;
wid1.show();
return app.exec();
}

关于c++ - 如何在 Mac OS X 10.5/10.6 上的 Qt 4.4 中接收来自 Apple 地址簿的拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1804728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com