Results 1 to 7 of 7

Thread: getting absolute file path of Qt resources

  1. #1
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default getting absolute file path of Qt resources

    I have some files in a .qrc file like this:

    Qt Code:
    1. <file>objs/cube.obj</file>
    To copy to clipboard, switch view to plain text mode 

    and I want to resolve the absolute path to that file into a char * array.

    I tried all sorts of combinations of QDir,QFile, and QFileInfo with no luck, does anyone have any ideas?

    Qt Code:
    1. //filename = ":/objs/cube.obj" here
    2. QFile file(fileName);
    3. file.open(QIODevice::ReadOnly);
    4. QFileInfo info(file);
    5. QByteArray ba = info.absoluteFilePath().toLatin1();//file.fileName().toLatin1();//
    6. const char *filename = ba.data();
    7. QMessageBox::about(this, tr("test"),tr("filename = %1").arg(filename) );
    8. //this outputs "filename = :/objs/cube.obj"
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting absolute file path of Qt resources

    At run time, the resources specified in the qrc are embedded in the binary file...
    When you distribute your app( binary ), you will not also distribute the resources.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting absolute file path of Qt resources

    Also, if you want to a set of resources dynamically, check QResource.

    Also:
    The resource files listed in the .qrc file are files that are part of the application's source tree. Paths are relative to the directory where the .qrc file is located. Resource data can either be compiled into the binary and thus accessed immediately in application code, or a binary resource can be created and at a later point in application code registered with the resource system.
    regards

  4. #4
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting absolute file path of Qt resources

    so I guess i misunderstood how this works. Just to clarify some more, I basically have some .obj files I want to embed in the app when i compile it, so i put it in the qrc file that has all my icons in it as well. The icons load up fine when i send ":/icon.png" to a QIcon or QPixmap constructor, but if I have a string called ":/cube.obj" and i load it like this:

    Qt Code:
    1. //filename = ":/cube.obj" at this point
    2. ifstream file;
    3. file.open(filename);
    To copy to clipboard, switch view to plain text mode 

    that won't work, so I am guessing that I need to change all the places I use ifstream over to QFile's. Would that make it work?

  5. #5
    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: getting absolute file path of Qt resources

    Quote Originally Posted by dvmorris View Post
    Would that make it work?
    Yes, it would.

  6. #6
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting absolute file path of Qt resources

    Qt Code:
    1. QFile file(":/objs/cube.obj");
    2. file.open(QIODevice::ReadOnly);
    3. QMessageBox::about(this, tr("asdf"),tr("%1").arg(file.exists()));
    To copy to clipboard, switch view to plain text mode 

    file.exists() is outputting 0 in the above code. What could I be doing wrong? my .qrc file has this line in it:

    Qt Code:
    1. <file alias="cube.obj">objs/cube.obj</file>
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: getting absolute file path of Qt resources

    ok nevermind, when i try ":/cube.obj" , the alias, it returns 1.

Similar Threads

  1. QMessageBox Icon Issue and How to add a dir to Qt Resources File
    By vishal.chauhan in forum Qt Programming
    Replies: 19
    Last Post: 25th March 2007, 00:13
  2. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  3. qtreeview -file path
    By onder in forum Newbie
    Replies: 3
    Last Post: 12th July 2006, 09:40
  4. Which path should I use?
    By Dark_Tower in forum Newbie
    Replies: 3
    Last Post: 8th April 2006, 12:09
  5. Set include file path in KDevelop
    By zlatko in forum KDE Forum
    Replies: 2
    Last Post: 16th January 2006, 11:12

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.