Get QObject with javascript failed!
-
mainwindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget parent = 0);
~MainWindow();
Q_INVOKABLE QObjectList all();
Q_INVOKABLE QObject get();
private:
Ui::MainWindow *ui;
QObjectList objects;
private slots:void addToJavaScriptWindowObject();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebKit>
#include <QWebFrame>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebSettings::globalSettings()->setAttribute( QWebSettings::DeveloperExtrasEnabled,true);QObject::connect(this->ui->webView->page()->mainFrame(),SIGNAL(javaScriptWindowObjectCleared()),this,SLOT(addToJavaScriptWindowObject())); //connect(this->ui->WV_Work->page()->mainFrame(),SIGNAL(loadFinished(bool)),this,SLOT(addToJavaScriptWindowObject())); //this->ui->webView->load(QUrl::fromLocalFile("D:/kuaipan/workspalce/projects/Zeus/forbid.html")); //th
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addToJavaScriptWindowObject()
{
this->ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("QObject",this);
//this->ui->webView->page()->mainFrame()->evaluateJavaScript("console.log(QObject);");
}
QObjectList MainWindow::all()
{
//QObjectList ret;return this->objects;
}
QObject* MainWindow::get()
{
return this;
}
@In Web Inspector's console ,i input " QObject.all() ",but it return
QObject.all()
Error: cannot call all(): unknown typeQObjectList' line: 2 message: "cannot call all(): unknown type
QObjectList'"
stack: "all@[native code]↵eval code↵eval@[native code]↵evaluate@[native code]↵_evaluateOn↵_evaluateAndWrap↵evaluate"
proto: Error -
Hi,
I'm not 100% sure, but try using QQmlListProperty instead of QObjectList: http://qt-project.org/doc/qt-5/qtqml-referenceexamples-properties-example.html