【问题标题】:get cursor position in WebEngineView qml在 WebEngineView qml 中获取光标位置
【发布时间】:2018-08-25 07:11:50
【问题描述】:

我在 Qt 中有一个程序,其中有一个 WebEngineView。我想当我的用户点击 webEngine 中的输入框时,一个键盘已经加载并且输入框从我的键盘获取它的内容(我写了我自己的键盘)但是我做不到。我尝试下面的代码但不起作用

WebEngineView {
    anchors.fill:parent
    id:webEng
    url: "https://example.com"
    visible: true
    MouseArea {
        id : mousearea
        anchors.fill: parent
        onClicked: {
           mykeyboard.visible=true;
        }  
    }
}

【问题讨论】:

  • 这个尝试看起来有点奇怪。如果您想从页面元素(在您的情况下为输入框)获取事件,为什么在单击所有页面时编写代码?您应该将逻辑基于页面本身的 Javascript 事件。使用WebChannelWebEngineScript
  • @folibis 对不起,你能举例一些我可以使用的 Javascript 事件吗

标签: qt qml


【解决方案1】:

这不是一个完整的答案,但这段代码可能会有所帮助:

import QtQuick 2.10
import QtWebView 1.1
import QtQuick.Controls 1.4
import QtWebEngine 1.7

Item {
    width: 1280
    height: 720

    WebView { // or WebEngineView {
        id: webview
        width: 1280
        height: 720
        url: "http://google.com"
        visible: true

        onLoadingChanged: {
            if (loadRequest.status === WebView.LoadSucceededStatus) {
                console.log("Loaded!!")

                webview.runJavaScript('
                    var input = document.getElementById("lst-ib");
                    input.addEventListener("click", function() {
                        alert("Clicked!");
                    });
                    '
                )
            }
        }
    }

    Rectangle {
        id: myDummyKeyboard
        anchors.bottom: parent.bottom
        width: parent.width
        height: 100
        color: "gray"
        visible: true
        border.width: 20
        Button {
            anchors.centerIn: parent
            text: "Dummy"
            onClicked: {
                webview.runJavaScript('document.getElementById("lst-ib").value += "' + text + '"');
            }
        }
    }
}
  • WebView(或WebEnginView)中的部分允许在单击输入时显示警报。但是,缺少一些东西,将其链接到 QML 处理程序。解决方案可能是使用 @folibis 在 cmets 中所说的 WebChannelWebEngineScript
  • myDummyKeyboard 定义的部分允许在用户单击矩形中的按钮时将字符串添加到输入中(假键盘)。

【讨论】:

  • 感谢您的帮助,但单击输入时不会显示任何警报,只会显示您的假键盘。对不起,我是 js 和 Qt 编程的初学者
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
相关资源
最近更新 更多