alexandernst
alexandernst

Reputation: 15099

Emiting events from JS and catching them in C++

I'm trying to make a movable window without actually creating any widgets (except a QWebView) and I'm wondering if is possible to emit QMouseEvent from JavaScript (inside QWebView) and catch the signal with C++?

I'm already aware how to call C++ methods from JS (and vice-versa) as described here but I'm asking if I'll be able to generate/emit an event containing a QMouseEvent (that's really important because of the current global position of the cursor).

Upvotes: 1

Views: 582

Answers (2)

alexandernst
alexandernst

Reputation: 15099

It wasn't that hard after all.

First of all, I exposed my window to the JavaScript engine:

this->webView()->page()->mainFrame()->addToJavaScriptWindowObject("CHtml5ApplicationViewer", this);

Then I created a simple div inside my HTML code and attached this code to handle the mouse onClick, onMove and onDrag events.

document.getElementById("bar").onmousedown = function(e){

    CHtml5ApplicationViewer.fakeMousePressEvent(e.pageX, e.pageY);
    e.preventDefault();

    document.onmousemove = function(e){
        CHtml5ApplicationViewer.fakeMouseMoveEvent(e.screenX, e.screenY);
        e.preventDefault();
    }

    this.onmouseup = function(){
        document.onmousemove = null;
    }

    document.getElementById("bar").ondragstart = function() { return false; }
}

Finally, I added two functions in my C++ code that will handle the calls from my JavaScript code:

void CHtml5ApplicationViewer::fakeMousePressEvent(int fromBorderX, int fromBorderY){
    fromBorderPosition = QPoint(fromBorderX, fromBorderY);
}

void CHtml5ApplicationViewer::fakeMouseMoveEvent(int dragX, int dragY){
    move(QPoint(dragX, dragY) - fromBorderPosition);
}

Also, make sure that you make those functions callable from JS!

public:
    Q_INVOKABLE void fakeMousePressEvent(int fromBorderX, int fromBorderY);
    Q_INVOKABLE void fakeMouseMoveEvent(int dragX, int dragY);

Upvotes: 2

Daniel MesSer
Daniel MesSer

Reputation: 1181

Almost certainly you can not do that since cpp is residing in another memory than the javascript running. However I have no idea what those tools are and how they are set up.

The usual way of dealing with those issues is to make some kind of "neutral" middle layer/communication channel by just sending strings/xml/json-objects or your own "script language". If you automate this process it would usually be called "serializing an object" which means converting it to some kind of format that you can either store on disk and rebuild later or convert it into "plain text" that some other tool can read and understand. An object resides in memory and as long as you don't share memory between the applications there is no way that they can understand what is happening.

For instance with your own "script language" always do calls through custom function notifyCPP(header, parameter) where an example would be notifyCPP("MOUSE_POSITION_UPDATE", "x=189; y=200");

Upvotes: 0

Related Questions