基于QT QWebEnginePage::setWebChannel() 传输对象 and Qt:无法从 javascript 调用共享对象方法/属性我尝试制作一个小演示来测试功能。请参阅下面的基本测试代码。我的问题是:
- 在新 QWebChannel 的 javascript 构建过程中,Qt 控制台中有大量打印“对象 '?' 的属性 '?''”没有通知信号并且不是恒定的,HTML 中的值更新将被破坏!”。这是怎么回事,我该如何解决?
- 当尝试从 javascript 端检索它们时,所有 C++ 对象属性均未定义。通过网络通道检索属性的正确方法是什么?
- C++ 函数的所有返回值都以未定义的形式返回...在 javascript 中接收 C++ 返回值的正确方法是什么?
。轮廓
QT += core gui
QT += network webenginewidgets webchannel widgets
TARGET = hfbTestWebChannel
TEMPLATE = app
SOURCES += hfbTestWebChannel.cpp
RESOURCES += hfbTestWebChannel.qrc
.html 文件
<html>
<body>
<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
<script type="text/javascript">
var theQtObj;
function buttonA()
{
PrintLog("Button A:init")
if (typeof qt !== "undefined")
{
new QWebChannel(qt.webChannelTransport, function(channel)
{
theQtObj = channel.objects.theQtObj;
PrintLog(" done, now theQtObj=" + theQtObj.toString());
});
}
else
PrintLog("!!!qt undefined!!!" );
}
function buttonB()
{
PrintLog("Button B : call c++ and get some answers!")
if(typeof theQtObj !== "undefined")
{
var X = prompt("Enter a number", "5");
PrintLog("theQtObj=" + theQtObj + " => X=" + X);
var n = theQtObj.getInt(X);
PrintLog(" back in js with n="+n);
var d = theQtObj.getDouble(n);
PrintLog(" back in js with d="+d);
var s = theQtObj.getString(d.toString());
PrintLog("X:" + X + "->" + n + "->" + d + "->" + s);
}
else
PrintLog(" --> theQtObj not defined");
}
function buttonC()
{
PrintLog("Button C:get c++ object member elements")
if(typeof theQtObj !== "undefined")
{
PrintLog("theQtObj._theString=" + theQtObj._theString);
PrintLog("theQtObj._theInt=" + theQtObj._theInt);
}
else
PrintLog(" --> theQtObj not defined");
}
var x=0;
function PrintLog(txt)
{
var myBox = document.getElementById("textBoxLog");
myBox.value += ("\n" + x + ": " + txt);
myBox.scrollTop = myBox.scrollHeight;
return (x++);
}
</script>
<p>Test Example to call Qt function from Javascript </p>
<p>
<input type="button" value=" A:init " onclick="buttonA()">
<input type="button" value=" B:get int " onclick="buttonB()">
<input type="button" value="C:get members" onclick="buttonC()">
</p>
<p>
<textarea id="textBoxLog" rows="31" cols="95">
textBoxLog
</textarea>
</p>
</body>
</html>
.cpp 文件
#include <QWebEngineView>
#include <QApplication>
#include <QtWebChannel/QtWebChannel>
/////////////////////////////////////////////////////////////////
class hfbDisplayWidget : public QWebEngineView
{
Q_OBJECT
public:
hfbDisplayWidget(QWidget * parent) : QWebEngineView(parent)
{
page()->setWebChannel(&_webchannel);
_webchannel.registerObject(QString("theQtObj"), this);
_theString = "Hello World";
_theInt = 777;
}
QWebChannel _webchannel;
Q_INVOKABLE QString _theString;
Q_INVOKABLE int _theInt;
Q_INVOKABLE int getInt(QVariant n)
{
int N = n.toInt();
QString js = QString("PrintLog('c++ getInt(%1) fired!')").arg(N);
_theString = js;
_theInt = N;
page()->runJavaScript(js, [=](const QVariant &rslt){
qDebug() << js << " -> " << rslt;
});
return N*100;
}
Q_INVOKABLE double getDouble(QVariant d)
{
double D = d.toDouble();
QString js = QString("PrintLog('c++ getDouble(%1) fired!')").arg(D);
page()->runJavaScript(js, [=](const QVariant &rslt){
qDebug() << js << " -> " << rslt;
});
return (D+0.12345);
}
Q_INVOKABLE QString getString(QVariant s)
{
QString S = s.toString();
QString js = QString("PrintLog('c++ getString(%1) fired!')").arg(S);
page()->runJavaScript(js, [=](const QVariant &rslt){
qDebug() << js << " -> " << rslt;
});
return (QString("c++ called with:'") + S + QString("'"));
}
};
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
hfbDisplayWidget view(nullptr);
view.setUrl(QUrl("qrc:/hfbTestWebChannel.html"));
view.show();
return a.exec();
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
#include "hfbTestWebChannel.moc"
*编辑添加所有代码以确保完整性
面临同样的问题......仔细阅读文档给了我答案。答案是Qt5中Qt和JS之间的通信是异步的。您必须提供回调函数,该函数将在方法完成并收到结果值后调用。
而不是打电话
var n = theQtObj.getInt(X);
PrintLog(" back in js with n="+n);
您可以拨打 as
theQtObj.getInt(X, function(n) {
PrintLog(" back in js with n="+n);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)