最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - qt.webChannelTransport undefined in QWebEngineView - Stack Overflow

programmeradmin2浏览0评论

I ran into a problem using QWebChannel for accessing an object from JavaScript. I'm currently using Qt5.4.2.

Here's my CPP code :

myObject::myObject(QWidget *parent)
: QMainWindow(parent)
{

    QWebEngineView* m_pView = new QWebEngineView(this);

    QWebChannel channel;
    channel.registerObject(QString("myObject"), this);

    m_pView->load(QUrl("file:///D:/index.html"));

    setCentralWidget(m_pView);

}

In my index.html, I am including qwebchannel.js :

<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>

And in my javascript file, I am trying to retrieve my object like this :

new QWebChannel(qt.webChannelTransport, function(channel) {

var myObject = channel.objects.myObject;

});

However, I get the following error in the console :

Error: qt is not defined

I also tried to replace it with navigator.qtWebChannelTransport but I got :

Error: transport is not defined

Can somebody tell me what did I do wrong? Thanks.

Edit : Is qt.webChannelTransport only accessible with Qt5.5? It seems to be the case when I read the doc of QWebEnginePage::setWebChannel...

I ran into a problem using QWebChannel for accessing an object from JavaScript. I'm currently using Qt5.4.2.

Here's my CPP code :

myObject::myObject(QWidget *parent)
: QMainWindow(parent)
{

    QWebEngineView* m_pView = new QWebEngineView(this);

    QWebChannel channel;
    channel.registerObject(QString("myObject"), this);

    m_pView->load(QUrl("file:///D:/index.html"));

    setCentralWidget(m_pView);

}

In my index.html, I am including qwebchannel.js :

<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>

And in my javascript file, I am trying to retrieve my object like this :

new QWebChannel(qt.webChannelTransport, function(channel) {

var myObject = channel.objects.myObject;

});

However, I get the following error in the console :

Error: qt is not defined

I also tried to replace it with navigator.qtWebChannelTransport but I got :

Error: transport is not defined

Can somebody tell me what did I do wrong? Thanks.

Edit : Is qt.webChannelTransport only accessible with Qt5.5? It seems to be the case when I read the doc of QWebEnginePage::setWebChannel...

Share Improve this question edited Feb 25, 2016 at 10:18 IAmInPLS asked Feb 25, 2016 at 10:13 IAmInPLSIAmInPLS 4,1254 gold badges28 silver badges60 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

You must setWebChannel before load url

Thats correct.

QWebChannel integration with QWebEngine is only available from version 5.5 as stated here by Milian, the main developer of the module.

You have to google qwebchannel.js to get the default code (it's a lot of code actually) or get it from out of Qt's directories somehow. I put mine under <qrc>/qtwebchannel/qwebchannel.js. Then make sure you import it as a regular javascript into your index.html but with source as "qrc:/qtwebchannel/qwebchannel.js". I had your exact error earlier today, and something I did fixed it - was probably including that script.

For others having the same issue but using Qt 5.5+, make sure you have QT += webchannel in your .pro file.

发布评论

评论列表(0)

  1. 暂无评论