Get QObject with javascript failed!



  • mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    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&#40;"D:/kuaipan/workspalce/projects/Zeus/forbid.html"&#41;);
    //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 type QObjectList' line: 2 message: "cannot call all(): unknown typeQObjectList'"
    stack: "all@[native code]↵eval code↵eval@[native code]↵evaluate@[native code]↵_evaluateOn↵_evaluateAndWrap↵evaluate"
    proto: Error



  • I experience the same problem, maybe even worse, because if I open the Javascript console through the inspector and I try to access the method returning the QObjectList it works Perfectly!
    Any idea?


  • Moderators

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.