Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot invoke shared object methods/properties from javascript
Forum Updated to NodeBB v4.3 + New Features

Cannot invoke shared object methods/properties from javascript

Scheduled Pinned Locked Moved General and Desktop
webengine qt5qwebchannel qwe
1 Posts 1 Posters 877 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gilson
    wrote on 28 Sept 2015, 05:42 last edited by
    #1

    I have tried QWebengine, QWebChannel as per the qt documentation for sharing C++ object with javascript. but I cant able to access any methods or properties in the c++ object.

    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()));
    // Slot for testing library function call
    connect ((QObject*)ui->pushButton,SIGNAL(clicked()),this,SLOT(OnTest())); 
    

    }

    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

    1 Reply Last reply
    0

    1/1

    28 Sept 2015, 05:42

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved