Important: Please read the Qt Code of Conduct -

QWebChannel - emitting a signal from JavaScript

  • I have an object exposed to JavaScript via QWebChannel. The object has the following signal: Q_SIGNAL void busy();. In C++ I have the signal connected to some slots. I would like to emit the signal from JavaScript (thus calling my C++ slots) however I can't find any way to do it.

    The JavaScript component of QWebChannel (qtwebchannel.js) generates an object for the signal (as seen here) so I can't call it directly. The only work around I've found so far is to have something like this in my C++ object:

    Q_INVOKABLE void signalBusy();
    Q_SIGNAL 	void busy();
    void MyClass::signalBusy()
    	emit busy();

    and use channel.objects.myObject.signalBusy(); in JavaScript. You can see how this might become rather annoying for objects with lots of signals.

    I guess the question is: is there a better way to do this ?

  • Did you ever find a solution so that you don't have to call a function that then emits a signal? I am running into this problem as well and we have several functions. The closest I've found are examples that only show how to connect a javascript function to a c++ signal.

Log in to reply