Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I get my JS running?



  • I am trying to execute my JS when ever any signal is emiited from Qt code. I am using QgraphicsWebview, but I am unable to figure out how can I do this. I have QPushbutton signal connected in my JS but when I click button my JS function does not get called. I am sure I am missing something in the code but can not figure out, can any one please point me to some document or example?



  • The official documentation is http://doc.qt.nokia.com/4.7/qtwebkit-bridge.html . If it still doesn't work, can you paste a small, compilable example that does not work?



  • Ah I did not know about addToJavaScriptWindowObject(), it is not mentioned in GraphicsWebView doc (atleast I could not see that) I was expecting it to work similar to QWebView which is running fine.
    Let me try to do it the way this documentation says.
    I will comeback again if it does not. Hopefully It will solve the problem:)



  • I added addToJavaScriptWindowObject() function hoping that I will get my signals in the JS but it is not working :(
    I did mainFrame()->addToJavaScriptWindowObject("mywidget",ObjName);
    and in my htm, i m calling mywidget.mysignal.connect(this,myJSFunction);
    But it is not calling myJSFunction(), I can see if I call slot of my object it works,
    mywidget.updateSlot(), I can see my debug is being printed there but signal which is being emiited from constructor is not connecting.



  • Here is my example:
    @
    #include <QtGui>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsWebView>
    #include<QWebFrame>

    class GrphWidget : public QGraphicsWidget
    {
    Q_OBJECT;
    public:
    GrphWidget();
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    public slots:
    void GrphWidgetSlot();

    signals:
    void paintSignal();
    void paintSignal2();
    };

    GrphWidget::GrphWidget()
    :QGraphicsWidget()
    {
    emit paintSignal();
    }

    void GrphWidget::GrphWidgetSlot()
    {
    emit paintSignal2();
    // emit mySignal2();
    }

    void GrphWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    painter->fillRect(boundingRect(), QColor(10,200,100));
    QGraphicsWidget::paint(painter, option, widget);
    }
    class MyWebPage : public QWebPage
    {
    Q_OBJECT;
    public:
    MyWebPage(QObject * parent = 0);
    protected:
    QObject * createPlugin(const QString & classid,const QUrl & url, const QStringList & var,const QStringList & values);
    };

    MyWebPage::MyWebPage(QObject * parent)
    : QWebPage(parent)
    {}

    QObject * MyWebPage::createPlugin(const QString & classid,const QUrl & url,const QStringList & var,const QStringList & values)
    {
    if (classid == "Object")
    {
    qDebug()<<" MyWebPage::createPlugin called";
    GrphWidget *myobj = new GrphWidget;
    QString name = values.at(var.indexOf("name"));
    mainFrame()->addToJavaScriptWindowObject(name,myobj);
    return myobj;
    }
    else
    {
    return NULL;
    }
    }
    #include"main.moc"
    int main(int argc, char *argv)
    {
    QApplication app(argc, argv);
    QUrl url("index.html");
    QGraphicsScene
    myscene = new QGraphicsScene(0, 0, 600, 400);
    QGraphicsWebView myview;
    myview.setGeometry(QRectF(0, 0, 600, 400));
    myscene->addItem(&myview);
    QGraphicsView view(myscene);
    MyWebPage webPage(&myview);
    myview.setPage(&webPage);
    myview.settings()->setAttribute(QWebSettings::PluginsEnabled, TRUE);
    webPage.setViewportSize(QSize(600,400));
    myview.load(url);
    view.show();
    return app.exec();

    }
    @

    my JS file:

    @
    <html>
    <head>
    [removed]
    function mySignalFunction()
    {
    alert("I got my signal");
    }
    [removed]
    </head>
    <body style="background-color:LightSkyBlue">
    If singals are connected then it should show my message box.
    <br>
    <object type="application/x-qt-plugin" classid="Object" name="myObject" height="40"
    width="100">
    </object>

    <object type="application/x-qt-plugin" classid="Object" name="myObject2" height="40"
    width="100">
    </object>
    <br>

    [removed]
    myObject.GrphWidgetSlot();
    myObject.mySignal.connect(this, mySignalFunction);
    myObject2.mySignal2.connect(this, mySignalFunction);

    [removed]
    </body>
    </html>
    @



  • Hi Immii,
    if you paste code, please surround it with @. Then it is displayed as code. You also find buttons over the edit area to insert these tags. I did it for your last post. Thanks.



  • Thank you Gerolf, I am very very new to this, today only I joined this Network, I did not know how to paste the code. Thank you for doing this for me.



  • Your welcome, an welcome on DevNet. I hope, you enjoy it and find the answers, you are searching.



  • Finally I got it working. Looks like signals slots were not being initialized properly. I put One alert in JS and every thing worked. So finally I did my connection at the end when loading of the page was done, so I added onload=myfunction() in body element and put all my connection there and every thing worked.


Log in to reply