QT 使用 addToJavaScriptWindowObject()
我正在尝试使用void QWebFrame::addToJavaScriptWindowObject(const QString & name, QObject * object)
。我的问题是当我尝试调用 JavaScript 中的函数时
类型错误:表达式“screen.valueChanged”[未定义]的结果不是函数。
TimeSliceScreen::TimeSliceScreen(QWidget* parent)
:
QWidget( parent )
{
QVBoxLayout* layout = new QVBoxLayout( this );
_timeSlice = new QWebView( this );
_timeSlice->setMinimumSize( 200,200);
QSizePolicy policy = _timeSlice->sizePolicy();
policy.setVerticalStretch(3);
_timeSlice->setSizePolicy(policy);
_timeSlice->settings()->setAttribute( QWebSettings::JavascriptEnabled, true );
_timeSlice->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
layout->addWidget( _timeSlice );
layout->addStretch();
layout->addSpacing( 20 );
_timeSlice->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
interface = new WebPageInterface();
connect( _timeSlice->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
this, SLOT(populateJavaScriptWindowObject()) );
}
void TimeSliceScreen::populateJavaScriptWindowObject(){
_timeSlice->page()->mainFrame()->addToJavaScriptWindowObject(QString("screen"),
interface);
}
WebPageInterface
是一个非常简单的类,它扩展了 QObject
并有一个名为 valueChanged
的插槽,这是我尝试调用的函数。
我的 JavaScript 是:
function call() {
screen.valueChanged();
}
从“我读过的所有内容”中调用它
<input type="hidden" id="maxhid" name="maxhid" value="{maxSlider}" onchange="call()"/>
,说这是执行此操作的方法,但它对我不起作用。
I am trying to use void QWebFrame::addToJavaScriptWindowObject(const QString & name, QObject * object)
. My problem is when I try and call the function in the JavaScript
TypeError: Result of expression 'screen.valueChanged' [undefined] is not a function.
TimeSliceScreen::TimeSliceScreen(QWidget* parent)
:
QWidget( parent )
{
QVBoxLayout* layout = new QVBoxLayout( this );
_timeSlice = new QWebView( this );
_timeSlice->setMinimumSize( 200,200);
QSizePolicy policy = _timeSlice->sizePolicy();
policy.setVerticalStretch(3);
_timeSlice->setSizePolicy(policy);
_timeSlice->settings()->setAttribute( QWebSettings::JavascriptEnabled, true );
_timeSlice->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
layout->addWidget( _timeSlice );
layout->addStretch();
layout->addSpacing( 20 );
_timeSlice->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
interface = new WebPageInterface();
connect( _timeSlice->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
this, SLOT(populateJavaScriptWindowObject()) );
}
void TimeSliceScreen::populateJavaScriptWindowObject(){
_timeSlice->page()->mainFrame()->addToJavaScriptWindowObject(QString("screen"),
interface);
}
WebPageInterface
is a very simple class that extends QObject
and has one slot called valueChanged
that is the function I am trying to call.
My JavaScript is:
function call() {
screen.valueChanged();
}
which gets called from
<input type="hidden" id="maxhid" name="maxhid" value="{maxSlider}" onchange="call()"/>
Everything I have read says that this is the way to do it, but it's not working for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为screen是js中的保留名称。尝试将名称更改为其他名称。否则看起来应该可以工作。
I think screen is a reserved name in the js. Try changing the name to something else. Otherwise looks like it should work.