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

QTextBrowser::setSource() with non existent Url



  • If I call

    QTextBrowser::setSource("file:non-existent-file.html")
    

    I see the output window warns me:

    QTextBrowser: No document for file:non-existent-file.html
    

    but there is no return result or other means to know this has happened. I wish to detect it, how should I do so?

    Should I rely on calling QTextBrowser::sourceType() == QTextDocument::UnknownResource after trying to set it? [EDIT Oh dear, I can't, for whatever reason does not exist in PySide2 for Qt 5.12.2 'PySide2.QtWidgets.QTextBrowser' object has no attribute 'sourceType'.]

    There is no simple/independent call to QUrl("file:non-existent-file.html").doesThisExist() [please note, .isValid() only tests for Url format validity], and I really don't fell like doing some QNetwork... stuff for this, deal with local files vs remote resources, etc.


  • Moderators

    @JonB
    QFile has a static member to check for a file existence

    https://doc.qt.io/qt-5/qfile.html#exists

    works fore local, network and qrc files

    Is that what you're looking for?


    Edit:
    just noticed you're looking for a network/html validation 🤔 That I don't know (Except QNetwork stuff)



  • @J-Hilk
    I think QTextBrowser::source() looks in a variety of places (e.g. QTextBrowser::searchPaths()), beyond where QFile looks. I wanted to know if setSource() failed, regardless of local/network location, URL format, permissions, bad file type/HTML can't be loaded, whatever which would make source not load successfully....

    I guess I will have to treat special case of local file and just test for existence where I expect it to be, it will have to do me for now. Unless anyone can suggest better....


Log in to reply