【问题标题】:Use relative url in QML Webview在 QML Webview 中使用相对 url
【发布时间】:2011-05-24 19:49:48
【问题描述】:

我正在尝试让 QML (main.qml) 加载本地 HTML 文件 index.html 使用 url: "file:///../../htmlfiles/index.html" 但它不起作用。

你能帮忙吗?

【问题讨论】:

    标签: qt url webview qml relative-path


    【解决方案1】:

    我最终使用 setContextProperty()qApp->applicationDirPath() 从 c++ 传递到 QML,并使用 url: "file:///" + applicationDirPath + "/htmlfiles/index.html" 解决了这个问题。

    【讨论】:

    • engine.rootContext()->setContextProperty("workingDirectory", QUrl::fromLocalFile(app.applicationDirPath())); 在 Qt 5 中。
    【解决方案2】:
    WebView {
            url: Qt.resolvedUrl( "html/index.html" )
            x: 0
            y: 0
            smooth: false
            anchors {
                top: window.top
                bottom: window.bottom
                left: window.left
                right: window.right
            }
        }
    

    这对我有用!

    【讨论】:

      【解决方案3】:

      尝试不使用file:///

      WebView {
          url: "../../htmlfiles/index.html"
          // [...]
      }
      

      【讨论】:

      • 我在 .pro 中使用 DEPLOYMENT 复制了 htmlfiles 目录,但由于某种原因,这个解决方案不起作用(也许我遗漏了一些东西)。我的 QML 是使用 .qrc 文件捆绑的,HTML 文件是使用上述方法复制的。是这个问题吗?
      • @pshah:如果 QML 文件被编译成程序二进制文件,而 HTML 文件位于二进制文件之外,则无法通过相对路径引用,因为文件位于两个不同的“文件系统”中"。
      【解决方案4】:

      有更简单的方法:

      WebView {
              id: translationsList
              anchors.fill: parent
              url: "qrc:/about.html"
              }
      

      【讨论】:

      • 正如 Radon 在 cmets 部分建议的那样,我认为这行不通。 "@pshah:如果 QML 文件被编译成程序二进制文件,而 HTML 文件位于二进制文件之外,则无法通过相对路径引用它,因为文件位于两个不同的“文件系统”中。
      【解决方案5】:

      尝试: 文件名="/etc/issue"; url = Qt.resolvedUrl( 文件名 );

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多