QT 文件拖拽问题

QT 文件拖拽问题

在mac10.10系统中使用qt拖拽功能,调试发现QUrl显示为”file:///.file/id=**“;

解决方法:将QUrl转换为mac可识别的系统路径,再转回来

void LeawoQUrlToCFURLRef(QUrl &resUrl)
{
    QByteArray filePath = resUrl.toEncoded();
    if ( filePath.startsWith("file:///.file/id=") )
    {
        CFStringRef pathStr =CFStringCreateWithCString(kCFAllocatorDefault
                                                        , filePath.data()
                                                        ,kCFStringEncodingUTF8);
        //for CFURLCreateFilePathURL create temp URL, tempUrl = "file:///.file/id="
        CFURLRef tempUrl =CFURLCreateWithString(kCFAllocatorDefault, pathStr,NULL);
        //convert tempUrl to system file path
        CFErrorRef error =0;
        CFURLRef fileUrl =CFURLCreateFilePathURL(kCFAllocatorDefault, tempUrl, &error);
        //fileUrl = "eg:file:///Volumes/DISK2_10_9_5/Users/"
        //get char * for Qt from CFURLRef
        CFStringRef tempStr =CFURLGetString(fileUrl);
        CFIndex length =CFStringGetLength(tempStr);
        CFIndex maxSize =CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
        char *str = (char *)malloc(maxSize);
//        memset(str, 0, sizeof(char)*(length + 1));
        CFStringGetCString(tempStr, str, maxSize,kCFStringEncodingUTF8);
        resUrl = QUrl(str);
    }
}

你可能感兴趣的:(mac)