Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. Get QObject with javascript failed!
Forum Update on Monday, May 27th 2025

Get QObject with javascript failed!

Scheduled Pinned Locked Moved Qt WebKit
3 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zhangzhenhu
    wrote on 3 Apr 2013, 08:26 last edited by
    #1

    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 type QObjectList'"
    stack: "all@[native code]↵eval code↵eval@[native code]↵evaluate@[native code]↵_evaluateOn↵_evaluateAndWrap↵evaluate"
    proto: Error

    1 Reply Last reply
    0
    • L Offline
      L Offline
      liuq
      wrote on 4 Feb 2014, 18:29 last edited by
      #2

      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?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JKSH
        Moderators
        wrote on 5 Feb 2014, 00:33 last edited by
        #3

        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

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved