Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Connect qwebkit and javascript problem

    Qt WebKit
    2
    4
    3082
    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.
    • A
      atena_2019 last edited by

      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);

      }

      @

      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

      1 Reply Last reply Reply Quote 0
      • A
        atena_2019 last edited by

        i put the statement javascript "MyWin.test();" in a try catch and the error is that :
        ReferenceError : can't find variable MyWin

        so now i am sure that the connection is wrong . but i do not know why:(

        1 Reply Last reply Reply Quote 0
        • D
          dangelog last edited by

          Quoting the docs:

          bq. 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.

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply Reply Quote 0
          • A
            atena_2019 last edited by

            thanks a lot . you are right

            1 Reply Last reply Reply Quote 0
            • First post
              Last post