开发者

QT using addToJavaScriptWindowObject()

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::populateJava开发者_运维百科ScriptWindowObject(){
   _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.


I think screen is a reserved name in the js. Try changing the name to something else. Otherwise looks like it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜