To emulate mousePressed, mouseReleased events of the QMouseEvents in QML WebEngineView item



  • I am trying to port from QGraphicsWebView to QtWebEngine (since QWebEngineView is widget, not able to interact through QML).

    I have a QQuickWidget, which in turn has a QML file set to it. This QML file include QtWebEngine's WebEngineView.

    I am using this WebEngineView to display my html pages.

    Now the issue is that I need to emulate the QMouseEvents just as it's in QGraphicsWebView (i.e.)
    MousePressed()
    MouseReleased()

    I need to show a context menu on selection of some text.
    I tried to include MouseArea inside the WebEngineView. But the expected behaviour is not consistent.
    At one instance, I get the control in OnPressed, onReleased signals but selection is disabled.
    At another instance meaning , on another Run the selection is enabled but control doesn't reach MouseArea since no signals are caught.

    readwidget::readwidget(QWidget *parent) : QQuickWidget(parent)
    {
        engine = QQuickWidget::engine();
        root = this->rootContext();
    
        /* Setting the scroll bar off since webview has the scroll enabled */
      //  this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    
        /* Setting the current object into context of QML source */
        root->setContextProperty("rw", this);
    
        /*Initializing the variables for the usage in QML*/
        QString content = "";
        QUrl baseUrl = QUrl("");
        QVariant pgContent(content);
        root->setContextProperty("content",pgContent);
        root->setContextProperty("baseUrl",baseUrl);
    
        /* Setting the QML source as ReaderView.qml to be executed from
         * current object */
         setSource(QUrl("qrc:/ReaderView.qml"));
        QQmlComponent component(engine);
        component.loadUrl(QUrl("qrc:/ReaderView.qml"));
        object = component.create(root);
        qDebug()<<"errors : "<<component.errors();
        setResizeMode(QQuickWidget::SizeRootObjectToView);
    
    }
    
    //ReaderView.qml
    import QtQuick 2.0
    
    Rectangle {
        id: rect1
        height: 600
        color: "white"
     Browserwindow {
            id: browserwindow
            height: parent.height - 30
            width:parent.width
            y:15
            clip:true
        }
    
        Rectangle {
            id:nextrect
            width: 100
            height: 100
            anchors.top: parent.top
            anchors.right: parent.right        
            anchors.rightMargin: 1
            anchors.topMargin: (parent.height / 2 )  - 40
            color: "transparent"
    
            Image {
                id: nextPage
                  width: 50
                  height: 50
                  source: "images/next_button.png"
                  visible: false
              }
    
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onEntered: nextPage.visible =  true
                onExited: nextPage.visible =  false
                onClicked: {
                     pgslider.visible = true
                    notesbox.visible = false
                    hidetables()
                    rw.loadnextpage()
                    browserwindow.hideMenus()
                }
            }
    
        }
    
        Rectangle {
            id:previousrect
            width: 100
            height: 100
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.topMargin: (parent.height / 2 )  - 40
            color: "transparent"
            Image {
                id: prevPage
                  width: 50
                  height: 50
                  source: "images/prev_button.png"
                  visible: false
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onEntered: prevPage.visible =  true
                onExited: prevPage.visible =  false
                onClicked: {
                     pgslider.visible = true
                    notesbox.visible = false
                    hidetables()
                    rw.loadpreviouspage()
                    browserwindow.hideMenus()
                }
            } 
        }
        Toolbar {
            id: tb
            width: parent.width
            height: 50
            anchors.top: parent.top
            color: "transparent"
        }
    
        Pageslider {
            id:pgslider
            width: parent.width -30
            height: 25
            x:15
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 4
        }
        ContextMenu {
            id : cntxtMenu
            visible: false
        }
    }
    
    //BrowserWindow.qml
    import QtQuick 2.0
    import QtQuick.Controls 1.3
    import QtWebEngine 1.0
    
    
    Rectangle {
        id:flick
        height: parent.height
        width: parent.width - 5
        property variant mypoint: Qt.point(10,10)
        property variant pressPoint: Qt.point(10,10)
        property variant releasePoint: Qt.point(10,10)
        property string pagecontents: ""
        property string url: ""
       WebEngineView {
            id: currentWebview
            objectName: "webView"
            //url: "http://www.google.co.in"
            anchors.fill: parent
            readonly property string htmlContent: content
            onHtmlContentChanged: loadHtml(htmlContent, baseUrl);
    
            MouseArea {
                id : mousearea
                anchors.fill: parent
    
                drag {
                    id : weDrag
                    target: currentWebview
    
                }
    
                onPressed: {
                    pressPoint.x = mouseX
                    pressPoint.y = mouseY
                    console.log("PressPoint : "+pressPoint)
    
                }
                onReleased: {
                    releasePoint.x = mouseX
                    releasePoint.y = mouseY
                    console.log("releasePoint : "+releasePoint)
                    if(pressPoint != releasePoint )
                    {                   
                        currentWebview.runJavaScript("window.getSelection()", function(result) { console.log("selected Text=" + result);} )
                    }
                }
    
            }
        }
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.