Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I get my JS running?
Forum Updated to NodeBB v4.3 + New Features

How do I get my JS running?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.6k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    Immii
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      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?

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

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Immii
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Immii
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Immii
            wrote on last edited by
            #5

            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>
            @

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              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.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Immii
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

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

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    Immii
                    wrote on last edited by
                    #9

                    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.

                    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