1

I have tried exactly same as the answer of Vicky Chijwani for this question QT QWebEnginePage::setWebChannel() transport object and everything fine but I cant able to invoke any methods or properties of jshelper.

kindly look at my code myclass.h

class MyClass : public QObject
{
    Q_OBJECT
public:
    MyClass(QObject *parent = 0);
    void print();

    int num;

signals:

public slots:
};

myclass.cpp

MyClass::MyClass(QObject *parent) : QObject(parent)
{
 num=100;
}

void MyClass::print()
{
    QMessageBox bx;
    bx.exec();
}

mywebengineview.h

class MyWebEngineView : public QWebEngineView
{
public:
    MyWebEngineView(QWidget *parent);

    MyClass helper;
};

mywebengineview.cpp

MyWebEngineView::MyWebEngineView(QWidget *parent): QWebEngineView(parent)
{
    QWebChannel *channel = new QWebChannel(page());
    channel->registerObject(QStringLiteral("jshelper"), &helper);load(QUrl::fromLocalFile(QFileInfo("../html/index.html").absoluteFilePath()));
    page()->setWebChannel(channel);
}

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    view = new MyWebEngineView(this);
    view->setGeometry(10, 10, 500, 500);
    view->load(QUrl::fromLocalFile(QFileInfo("../html/index.html").absoluteFilePath()));
}

finally javascript

<script type="text/javascript">
        var jshelper;
        document.addEventListener("DOMContentLoaded", function () {
            new QWebChannel(qt.webChannelTransport, function(channel) {
                alert('ok');
                // all published objects are available in channel.objects under
                // the identifier set in their attached WebChannel.id property

                jshelper = channel.objects.jshelper;
                alert( jshelper.num );
                jshelper.print();

            });
        });

        </script>

The problem is these two lines never executes properly

alert( jshelper.num ); // gives 'undefined' message
jshelper.print(); //will not work

what is wrong with my code, I am trying to fix this issue around 4 days but I couldn't able to fix it.

4
  • 1
    What does console.log(channel.objects.jshelper), or console.log(channel.objects) say? Commented Sep 25, 2015 at 14:31
  • jshelper:__id__: "jshelper"objectSignals: Object__propertyCache__: ObjectdeleteLater: function () {destroyed: Objectconnect: function (callback) {disconnect: function (callback) {proto: ObjectobjectName: (...)get objectName: function () {set objectName: function (value) {objectNameChanged: ObjectpropertyUpdate: function (signals, propertyMap)signalEmitted: function (signalName, signalArgs)unwrapProperties: function ()unwrapQObject: function (response)__proto__: QObject Commented Sep 25, 2015 at 14:56
  • So no num or print functions clearly. Commented Sep 25, 2015 at 14:59
  • yes what i am missing..? i couldn't able to figure it out. Commented Sep 27, 2015 at 10:53

2 Answers 2

3

I know under QWebKit, I had to make the members functions look like this:

public slots:
    Q_INVOKABLE void print();
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a ton man. qtwebengine also require Q_INVOKABLE. I never tried this. :( Now its working.
3

I know I'm providing a comment late, but if the function in question is declared in the slots section, you don't have to use Q_INVOKABLE. Slightly less typing if there are several functions.

class MyClass : public QObject
{
    Q_OBJECT
public:
    MyClass(QObject *parent = 0);

slots:
    void print();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.