Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calling c++ function from qml
Forum Updated to NodeBB v4.3 + New Features

Calling c++ function from qml

Scheduled Pinned Locked Moved QML and Qt Quick
25 Posts 5 Posters 27.1k 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.
  • R Offline
    R Offline
    rockstar
    wrote on last edited by
    #1

    i am trying to call a function from qml but i am getting following error in qml file:

    result of expression cppClass.getmouseposition() [undefined] is not a function

    in c++ file i written following code:

    class A{ getmouseposition(){} };

    A sample;
    view.rootContext->setContextProperty("cppClass",&sample);

    please help me

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      Your method has to be either a slot or marked as Q_INVOKABLE. See the "documentation":http://developer.qt.nokia.com/doc/qt-4.8/qml-extending.html#methods.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rockstar
        wrote on last edited by
        #3

        ya i declared it as

        Q_INVOKABLE QString getmouseposition()

        but i still get error

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          Of course you do, because your class does neither inherit from QObject, nor does it contain the Q_OBJECT macro. See the "documentation":http://developer.qt.nokia.com/doc/qt-4.8/metaobjects.html.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rockstar
            wrote on last edited by
            #5

            ok...now that error has gone ...now new error is

            undefined reference to vtable for A

            A is my class name...

            please help me....sorry if i am troubling u with my questions.......

            1 Reply Last reply
            0
            • JohanSoloJ Offline
              JohanSoloJ Offline
              JohanSolo
              wrote on last edited by
              #6

              Try to clean your environment and build your project again.

              `They did not know it was impossible, so they did it.'
              -- Mark Twain

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rockstar
                wrote on last edited by
                #7

                Johan....I cleaned it but i am still getting that error.....

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  Re-run qmake and clean / rebuild your project.

                  If you have defined the class within a <code>.cpp</code> file, you will have to <code>#include "filename.moc"</code> as well.

                  If you still experience problems just paste your code here (or on pastebin), so we can take a look at it.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rockstar
                    wrote on last edited by
                    #9

                    this is main.cpp file*

                    @#include <QtGui/QApplication>
                    #include "qmlapplicationviewer.h"
                    #include <QDeclarativeView>
                    #include <QDeclarativeComponent>
                    #include <QDeclarativeEngine>
                    #include <QDebug>
                    #include <QDeclarativeContext>
                    class Sample:public QObject
                    {
                    Q_OBJECT
                    public:
                    int32_t x;
                    int32_t y;
                    Q_INVOKABLE QString getmouseposition() const
                    {
                    QDeclarativeEngine *engine=new QDeclarativeEngine;
                    QDeclarativeComponent component(engine,QUrl::fromLocalFile(("qml/justforTest/main.qml")));
                    QObject *rect=component.create();
                    QObject *mousearea=rect->findChild<QObject *>("mouse");
                    //qDebug()<<"position:"<<mousearea->property("mouseX").toInt()<<","<<mousearea->property("mouseY").toInt();
                    return mousearea->property(("mouseX")).toString();
                    }
                    };

                    Q_DECL_EXPORT int main(int argc, char *argv[])
                    {
                    QScopedPointer<QApplication> app(createApplication(argc, argv));
                    QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());

                    viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
                    viewer->setMainQmlFile&#40;QLatin1String("qml/justforTest/main.qml"&#41;);
                    viewer->showExpanded();
                    
                    QDeclarativeView view;
                    Sample sample;
                    QDeclarativeContext *qdc=view.rootContext();
                    qdc->setContextProperty("cppClass",&sample);
                    view.setSource(QUrl::fromLocalFile&#40;"qml/justforTest/main.qml"&#41;);
                    view.show();
                    return app->exec&#40;&#41;;
                    

                    }
                    @

                    this is main.qml file
                    @import QtQuick 1.0
                    Rectangle
                    {
                    width: 360
                    height: 360
                    Text {
                    id: txt
                    anchors.centerIn: parent
                    }
                    MouseArea
                    {
                    id:mouse
                    anchors.fill: parent
                    onClicked: console.log(cppClass.getmouseposition())

                    }
                    

                    }@

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #10

                      [quote author="Lukas Geyer" date="1327397248"]
                      If you have defined the class within a <code>.cpp</code> file, you will have to <code>#include "filename.moc"</code> as well.
                      [/quote]
                      ... <code>filename.moc</code> is <code>main.moc</code> in your case.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        rockstar
                        wrote on last edited by
                        #11

                        so i need to include main.moc in main.cpp file?

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lgeyer
                          wrote on last edited by
                          #12

                          Yes, you will have to <code>#include "main.moc"</code> as well.

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            rockstar
                            wrote on last edited by
                            #13

                            Lukas, when i include it, it is saying that

                            main.moc:no such file or directory

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lgeyer
                              wrote on last edited by
                              #14

                              What kind of toolset are you using? qmake should add a call to <code>moc</code> automatically. You might take a look at your compile output if <code>moc</code> is acutally called.

                              If it is just Qt Creator which complains about the file missing this is absolutely correct. The file is generated at the time you compile your application and thus cannot be found earlier. Just compile your application.

                              If you are using Visual Studio you might need to add this step manually (a how to can be found in the forums).

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                rockstar
                                wrote on last edited by
                                #15

                                Lukas, can u provide me a small example of how to call c++ functions from qml...this is what i am trying from morning and i am still unsuccessfull........and u asked me about toolset.....actually i am new to qml and qt....so i don't what it is exactly.....

                                1 Reply Last reply
                                0
                                • L Offline
                                  L Offline
                                  lgeyer
                                  wrote on last edited by
                                  #16

                                  Are you using Qt Creator? If so, if it is just Qt Creator, which complains about the file missing, just build and run your application. The file you are trying to include is generated on-the-fly when the application is built, and it is absolutely normal that it isn't available at development time.

                                  !http://img208.imageshack.us/img208/766/filenotfounderror.png! This means you are fine.

                                  Usually classes in general and classes subclassing QObject in particular are defined in <code>.h</code> files and implemented in <code>.cpp</code>. This way you do not need to mess around with including any generated files.

                                  1 Reply Last reply
                                  0
                                  • R Offline
                                    R Offline
                                    rockstar
                                    wrote on last edited by
                                    #17

                                    Lukas, i did as u told me....i included "main.moc"......i cleaned environment......i rebuilt the project and it is still prompting the same error on "build issues" panel ........

                                    1 Reply Last reply
                                    0
                                    • L Offline
                                      L Offline
                                      lgeyer
                                      wrote on last edited by
                                      #18

                                      Well, you then either

                                      • do it the way it is supposed to be done or
                                        [quote author="Lukas Geyer" date="1327401808"]Usually classes in general and classes subclassing QObject in particular are defined in <code>.h</code> files and implemented in <code>.cpp</code>. This way you do not need to mess around with including any generated files.[/quote]
                                      • you pack and upload your current application and link it here, so I can take a look on it.
                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        rockstar
                                        wrote on last edited by
                                        #19

                                        http://www.mediafire.com/?b9a9r9t6qq9aoao

                                        i uploaded the project.....have a look at it.....thanks in advance......

                                        1 Reply Last reply
                                        0
                                        • L Offline
                                          L Offline
                                          lgeyer
                                          wrote on last edited by
                                          #20

                                          <code>main.moc</code> is generated as expected and the project compiles fine using MinGW and MSVC2010. Be aware that you have to <code>#include "main.moc"</code> at the end of the source file (or at least after the class definition).

                                          I don't know what's the problem on your end.

                                          Is there any reason you are not just doing it as it is supposed to be done?

                                          [quote author="Lukas Geyer" date="1327401808"]Usually classes in general and classes subclassing QObject in particular are defined in <code>.h</code> files and implemented in <code>.cpp</code>. This way you do not need to mess around with including any generated files.[/quote]

                                          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