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. Connect qwebkit and javascript problem
QtWS25 Last Chance

Connect qwebkit and javascript problem

Scheduled Pinned Locked Moved Qt WebKit
4 Posts 2 Posters 3.3k 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.
  • A Offline
    A Offline
    atena_2019
    wrote on last edited by
    #1

    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
    0
    • A Offline
      A Offline
      atena_2019
      wrote on last edited by
      #2

      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
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        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
        0
        • A Offline
          A Offline
          atena_2019
          wrote on last edited by
          #4

          thanks a lot . you are right

          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