Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reading files from resources



  • Hello!

    I am trying to load a template from resources make a docx file (using docx factory lib). If I copy the template in the program folder, it reads successfully but, when I try to read it from resources, it can't read it.

    I have the same problem when I try to add an image in this docx. Is it possible to read this kind of files from the resources?

    /** TEMPLATE **/
    QString sTemplatePath = QApplication::applicationDirPath() + "/template.dfw";    // works fine
    QString sTemplatePath = ":/templates/resources/templates/template.dfw" // can't read the file
    
    l_merger.load(sTemplatePath.toStdString());
    
    ...
    
    /** IMAGES **/
    sImagePath = QCoreApplication::applicationDirPath() + "/image.png";   // works fine
    sImagePath = ":/images/resources/images/image.png";  // can't read the file
    
    l_merger.setClipboardValue("Header", "Image", sImagePath.toStdString());
    l_merger.paste("Header");
    ...
    

    Thank you very much!


  • Lifetime Qt Champion

    Hi,

    Do you have that folder structure in your resources ?



  • Yes, when I want to use this images for example in the stylesheet it works fine.


  • Lifetime Qt Champion

    @ivanicy
    Hi
    And you find the DFW file in the explorer tree under ressources
    and right click and take path via menu so you are 10000% sure its correct path ?



  • I took this path rightclicking on the resources file and selecting "Copy Path" option so the path is correct. I try with the "Copy URL" option too but with the same result.

    If I set a QPushButton icon with this paths, it works.


  • Lifetime Qt Champion

    @ivanicy
    But are you pasting it directly to the clipboard ?
    So when other app reads/see it,
    it will be the resource path which only a Qt app can understand ?


  • Lifetime Qt Champion

    See this:

    @ivanicy said in Reading files from resources:

    l_merger.load(sTemplatePath.toStdString());

    Looks like l_merger is not a Qt class, is it ? If so, then it won't be able to read from the resources because it has not idea on how to access it.

    In that case, you should create a temporary file, pass the path l_merger and continue processing.



  • @SGaist Yes, I think this could be the problem. It is a third party library so, maybe what is happening herer is what you say. I get the same error trying to use this image with opencv.


  • Lifetime Qt Champion

    @ivanicy
    Hi
    Yes, only QFile understands resources so if openCV uses normal file functions , it cannot load it.
    You have to save to a real file first then and use that path.



  • @mrjj @SGaist Thank you both of you!


Log in to reply