Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Get QObject with javascript failed!

    Qt WebKit
    3
    3
    1504
    Loading More Posts
    • 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
      zhangzhenhu last edited by

      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 Reply Quote 0
      • L
        liuq last edited by

        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 Reply Quote 0
        • JKSH
          JKSH Moderators last edited by

          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 Reply Quote 0
          • First post
            Last post