【问题标题】:Qt how to insert html into an editable QWebView at the cursor position?Qt如何将html插入光标位置的可编辑QWebView?
【发布时间】:2013-07-25 17:39:42
【问题描述】:

我正在尝试使用QWebView 来实现博客文章编辑器。我有一些示例 html sn-ps 通过触发菜单操作插入到编辑器中。但是,插入html并不像QTextEdit那样方便。至于我为什么不用QTextEdit,看我的测试代码如下:

QTextEdit *edit = new QTextEdit;
edit->insertHtml(tr("<div class=\"gci-hello\">Hello</div>"));
qDebug() << edit->toHtml(); // --> the div tag disappeared

所以,如果我使用 QWebView,div 标签将被保留。但我不知道如何在视图上的光标位置插入我的 sn-p。

【问题讨论】:

    标签: qt qwebview qwebkit


    【解决方案1】:

    execCommandInsertHTML 一起使用:

    QString html = "<div>Some text</div>";
    QString js = QString("document.execCommand('InsertHTML',false,'%1');").arg(html);
    webview->page()->mainFrame()->evaluateJavaScript(js);
    

    如果 HTML sn-p 中有单引号字符,请确保用反斜杠引用它们,因为 sn-p 是通过 JS 字符串注入的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 2016-08-01
      • 2022-07-14
      • 2019-09-25
      • 1970-01-01
      相关资源
      最近更新 更多