qtwebkit并连接cpp和javascript [英] qtwebkit and connect cpp and javascript
本文介绍了qtwebkit并连接cpp和javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我正在使用qwebkit
我有一个cpp对象,并将其导出到javascript,但由于无法正常工作而无法连接
这是我的代码:我的cpp代码:
hi to all
i am using qwebkit
i have a cpp object and export it to javascript but it does not connect to it , because it does not work
here is my code : my cpp code :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qwebframe>
#include <qmessagebox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setObjectName("MyWin");
connectCPPObjectToJS(this->objectName() , this);
loadUrl(QCoreApplication::applicationDirPath() + "/sura.htm");
}
void MainWindow::test()
{
QMessageBox::information(this , "a" , "hello");
}
void MainWindow::loadUrl(const QString & localpath)
{
ui->webView->load(QUrl::fromLocalFile(localpath));
}
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
QString s ;
QMessageBox::information(this , "index change" , s.setNum(index));
emit sendChangedIndexToJS(index);
}
void MainWindow::connectCPPObjectToJS(const QString objectname , QObject* objecttype)
{
ui->webView->page()->mainFrame()->addToJavaScriptWindowObject( objectName() , objecttype);
}
void MainWindow::on_comboBox_textChanged(QString a)
{
QMessageBox::information(this , "text change" , a);
这是我的html代码:
and here is my html code :
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>مثال چهار قل</title>
[removed]
function updatesura(value){
alert("the value was changed to " + value);
}
MyWin.test();
MyWin.sendChangedIndexToJS.connect(updatesura());
[removed]
</head>
<body>
<p align="center"><font face="Tahoma">سوره</font>
</p>
</body>
</html>
怎么了?
非常感谢
what is wrong ?
thanks a lot
推荐答案
我找到了答案:
如果要确保QObject在加载新URL后仍然可访问,则应将它们添加到连接到javaScriptWindowObjectCleared()信号的插槽中.
i found the answer :
If you want to ensure that your QObjects remain accessible after loading a new URL, you should add them in a slot connected to the javaScriptWindowObjectCleared() signal.
这篇关于qtwebkit并连接cpp和javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文