QT 使用 addToJavaScriptWindowObject()

发布于 2024-12-04 13:28:17 字数 1763 浏览 3 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-12-11 13:28:17

我认为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文