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

How do setSource() and setSearchPaths() in QTextBrowser work together?



  • One function takes a QUrl object as parameter, the other a QStringList. The documentation says this about the QTextBrowser::source property:
    "When setting this property QTextBrowser tries to find a document with the specified name in the paths of the searchPaths property and directory of the current source, unless the value is an absolute file path. It also checks for optional anchors and scrolls the document accordingly."

    I have some HTML files embedded in the Qt resource file. I have defined virtual folders "/help" and "help/images". So when I load the HTML file and write:

    ui->browser->setSource(QUrl("qrc:/help/index.html"));
    

    it works. However, if I set searchPaths to "qrc:/help" and "qrc:/help/images", then write:

    ui->browser->setSource(QUrl("index.html"));
    

    it cannot find the file. I haven't tried any images yet, because I do not know how I am supposed to do this. There will eventually be more paths depending on languages.


  • Qt Champions 2019

    @robert-hairgrove I guess setSearchPaths() only works for file system, not resources.



  • In the meantime, I tried loading a page with one image. As long as I include the subdirectory in the image src tag, it is found, even without setting searchPaths at all. The image and the HTML page are actually in the same physical qrc folder (as required by Qt, at least -- actually I am putting symlinks to the real files there so that I have a little more organization in my work). So it appears that the virtual folders defined in the resources are found OK.

    Then the question remains: What can searchPaths do for me? Does someone have an example?


  • Qt Champions 2019

    searchPaths() is using QFileInfo(foo).isReadable() - I would check what this gives for you to see if maybe QFileInfo can't handle qrc urls.

    See https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qtextbrowser.cpp.html#_ZNK19QTextBrowserPrivate8findFileERK4QUrl



  • @christian-ehrlicher Thanks for this useful link. Perhaps I should use the ":/" scheme when setting searchPaths? The findFile() function transforms "qrc:/" into ":/" before doing the actual search. I will try this.


Log in to reply