找回密码
 立即注册
收起左侧

如何动态改变webView中的内容

0
回复
5378
查看
[复制链接]

尚未签到

来源: 2015-5-31 19:04:16 显示全部楼层 |阅读模式

马上注册,查看详细内容!注册请先查看:注册须知

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
我想要用qml+python做一个 markdown 编辑器,在QWebView中有个setHtml的方法,可以改变QWebView的显示内容,在qml中webView的内容怎么改变呢?

下面是代码,主要看Mytab.qml 就好啦。


main.qml
  1. import QtQuick 2.2
  2. import QtQuick.Controls 1.1
  3. import QtQuick.Layouts 1.1

  4. ApplicationWindow{
  5.                 visible:true
  6.                 width:640
  7.                 height:480
  8.                 id:window
  9.                 title:"markdwon editor"

  10.                 signal textUpdated(string text)

  11.     Action {
  12.         id: exitAction
  13.         text: qsTr("E&xit")
  14.         onTriggered: Qt.quit()
  15.     }
  16.     Action {
  17.         id: newAction
  18.         text: qsTr("New")
  19.         iconSource: "images/new.png"
  20.         onTriggered: {
  21.             
  22.                 var c = Qt.createComponent("Mytab.qml");
  23.                 tabview.addTab("tab", c);
  24.                 var last = tabview.count-1;
  25.                 tabview.getTab(last).active = true; // Now the content will be loaded
  26.                 //console.log(tabview.getTab(last).item);
  27.         }
  28.     }
  29.     Action {
  30.         id: cutAction
  31.         text: qsTr("Cut")
  32.         iconSource: "images/cut.png"
  33.         onTriggered: textArea.cut()
  34.     }
  35.     Action {
  36.         id: copyAction
  37.         text: qsTr("Copy")
  38.         iconSource: "images/copy.png"
  39.         onTriggered: textArea.copy()
  40.     }
  41.     Action {
  42.         id: pasteAction
  43.         text: qsTr("Paste")
  44.         iconSource: "images/paste.png"
  45.         onTriggered: textArea.paste()
  46.     }
  47.     Action {
  48.         id: selectAllAction
  49.         text: qsTr("Select All")
  50.         onTriggered: textArea.selectAll()
  51.     }

  52.     menuBar: MenuBar {
  53.         Menu {
  54.             title: qsTr("&File")
  55.             MenuItem { action: newAction }
  56.             MenuItem { action: exitAction }
  57.         }
  58.         Menu {
  59.             title: qsTr("&Edit")
  60.             MenuItem { action: cutAction }
  61.             MenuItem { action: copyAction }
  62.             MenuItem { action: pasteAction }
  63.             MenuSeparator {}
  64.             MenuItem { action: selectAllAction }
  65.         }
  66.     }

  67.     toolBar: ToolBar {
  68.         Row {
  69.             anchors.fill: parent
  70.             ToolButton { action: newAction }
  71.             ToolButton { action: cutAction }
  72.             ToolButton { action: copyAction }
  73.             ToolButton { action: pasteAction }
  74.         }
  75.     }




  76.        
  77.        

  78.         TabView{
  79.             anchors.fill: parent
  80.             visible: true
  81.             id:tabview
  82.             Layout.fillHeight: true
  83.             Layout.fillWidth: true
  84.         }
  85.    
  86. }



复制代码
Mytab.qml
  1. import QtQuick 2.2
  2. import QtQuick.Controls 1.1
  3. import QtQuick.Layouts 1.1
  4. import QtWebKit 3.0

  5. Rectangle{
  6.         id:tab
  7.         width:parent.width
  8.         height:parent.height
  9.        



  10.     Row {
  11.         id: row
  12.         anchors.fill: parent
  13.         spacing: 8
  14.         
  15.         Rectangle{
  16.                 width:parent.width/2
  17.                 height:parent.height
  18.                 TextArea {
  19.                         id: textArea
  20.                         text:""
  21.                         anchors.fill: parent
  22.                         onTextChanged: {
  23.                                
  24.                                 htmlChanged(textUpdated(text))
  25.                         }
  26.                 }

  27.         }

  28.         Rectangle{
  29.                 width:parent.width/2
  30.                 height:parent.height
  31.                 WebView {
  32.                         id: web
  33.                         url:""
  34.                         anchors.fill: parent
  35.                 }
  36.         }
  37.     }

  38.     function htmlChanged(html){
  39.        
  40.     }
  41. }
复制代码
main.py
  1. import sys
  2. from PyQt5.QtCore import QObject, QUrl, Qt
  3. from PyQt5.QtWidgets import QApplication
  4. from PyQt5.QtQml import QQmlApplicationEngine
  5. import markdown

  6. def toHtml(text):
  7.         return markdown.markdown(text)


  8. if __name__ == "__main__":
  9.   app = QApplication(sys.argv)
  10.   engine = QQmlApplicationEngine()

  11.   

  12.   engine.load('main.qml')

  13.   win = engine.rootObjects()[0]

  14.   win.textUpdated.connect(toHtml)
  15.   win.show()
  16.   sys.exit(app.exec_())
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了