Results 1 to 3 of 3

Thread: Qt embedded Objective C++ code not working properly

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Qt embedded Objective C++ code not working properly

    I need to call some Objective C API from my C++ code. So I created two files - Foo.mm and Foo.h, and included them in the project pro file as

    Qt Code:
    1. macx {
    2. HEADERS += Foo.h
    3. OBJECTIVE_SOURCES += Foo.mm
    4. }
    To copy to clipboard, switch view to plain text mode 

    I am dragging and dropping a file on my main window, and I need the actual path of that file, but there is a bug in Qt-Yosemite combination, due to which some bizarre path is being printed, so I need to call native APIs to make it work, as discussed here. So here is my code in Foo.mm:

    Qt Code:
    1. QString returnActualPathForDroppedFile(QString aPath)
    2. {
    3. QUrl url(aPath);
    4.  
    5. qDebug() << "First: " << url.toString().toStdString().c_str() << endl; //prints correctly
    6. qDebug() << "Orig: " << aPath.toStdString().c_str() << endl; //prints correctly
    7.  
    8. if (url.host().toLower() == QLatin1String("localhost"))
    9. url.setHost(QString());
    10. if (url.host().isEmpty() && url.path().startsWith(QLatin1String("/.file/id=")))
    11. {
    12. url = QUrl::fromNSURL([url.toNSURL() filePathURL]);
    13. qDebug() << "Inside: " << url.toString().toStdString().c_str() << endl; //this one prints it blank
    14. }
    15. url.setPath(url.path().normalized(QString::NormalizationForm_C));
    16.  
    17. qDebug() << "Here: " << url.toString().toStdString().c_str() << endl; //thus it is blank as well
    18. return url.toString();
    19. }
    To copy to clipboard, switch view to plain text mode 

    After I call the method from my C++ code with the file path of the file being dropped, it comes here, but after the call to the filePathURL method, it returns a blank, whereas as per the patch, it should return the correct path (I once compiled Qt from source using the patch, it works fine, so the patch is correct).

    The error message I get is this: CFURLCreateFilePathURL failed because it was passed this URL which has no scheme: /.file/id=6571367.17566290

    How do I fix this?

    Platform - Qt 5.3.1 32 bit, OS X Yosemite.
    Last edited by Cupidvogel; 16th April 2015 at 21:46.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt embedded Objective C++ code not working properly

    The URL obviously lacks the scheme. Have you tried file:///?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Cupidvogel (17th April 2015)

  4. #3
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Qt embedded Objective C++ code not working properly

    I actually solved the problem right now. Here is the solution:

    Qt Code:
    1. QString returnActualPathForDroppedFile(QString aPath)
    2. {
    3. QUrl url(aPath);
    4. if (url.host().toLower() == QLatin1String("localhost"))
    5. url.setHost(QString());
    6. if (url.host().isEmpty() && url.path().startsWith(QLatin1String("/.file/id=")))
    7. {
    8. const QByteArray utf8 = aPath.toUtf8();
    9. const char* cString = utf8.constData();
    10. NSString *localFilePath = [[NSString alloc] initWithUTF8String:cString];
    11. NSURL *localFilePathURL = [NSURL fileURLWithPath:localFilePath];
    12. url = QUrl::fromNSURL([localFilePathURL filePathURL]);
    13. }
    14. url.setPath(url.path().normalized(QString::NormalizationForm_C));
    15. return url.toString();
    16. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QT embedded widget demo code
    By jhowland in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2009, 03:39
  2. Qt Embedded messy code displayed on the screen
    By iskevin in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 12th November 2009, 01:29
  3. Objective C, Python and Ruby code with C++ Qt application
    By Berberis in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2008, 12:40
  4. Profiling of code for embedded systems
    By dbugger in forum Newbie
    Replies: 1
    Last Post: 3rd April 2008, 19:25
  5. QT and Objective-C
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2006, 19:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.