2011-05-24 26 views

回答

5

我結束了使用setContextProperty()傳遞qApp->applicationDirPath()從C++到QML和使用url: "file:///" + applicationDirPath + "/htmlfiles/index.html"來解決該問題。

+0

+1:就是這麼做的方法 – funkybro 2011-06-05 07:45:42

+0

'在Qt 5中'engine.rootContext() - > setContextProperty(「workingDirectory」,QUrl :: fromLocalFile(app.applicationDirPath()));' – Timmmm 2014-11-17 14:37:57

2

嘗試沒有file:///

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

我在.pro中使用DEPLOYMENT複製了htmlfiles目錄,出於某種原因,此解決方案無效(也許我錯過了某些內容)。我的QML使用.qrc文件捆綁在一起,並使用上述方法複製HTML文件。這是問題嗎? – pshah 2011-05-26 15:20:47

+0

@pshah:如果QML文件被編譯到程序二進制文件中,並且HTML文件位於二進制文件之外,那麼它不能被相對路徑引用,因爲這些文件位於兩個不同的「文件系統」中。 – hiddenbit 2011-05-26 16:51:22

1

有做簡單的方法是:

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

正如Radon在評論中提出的部分,我認爲這是行不通的。 「@pshah:如果QML文件被編譯到程序二進制文件中,並且HTML文件位於二進制文件之外,則它不能被相對路徑引用,因爲這些文件位於兩個不同的」文件系統「中。 – pshah 2011-09-02 04:08:12

0

嘗試: =文件名 「的/ etc /問題」; url = Qt.resolvedUrl(filename);

3
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 
     } 
    } 

這對我有用!