Results 1 to 4 of 4

Thread: QML and C++ interaction

  1. #1
    Join Date
    Sep 2010
    Posts
    14
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

    Default QML and C++ interaction

    Hi,

    I'm trying to get an access to my c++ class from within QML. The idea is to run a function in C++ side when I press a rectangle area in QML. Also sending a signal from qml and catching it in c++ is an option. But all the examples I've found with google don't seem to work. I.e:

    Qt Code:
    1. QDeclarativeView view;
    2. view.setSource(QUrl::fromLocalFile("main.qml"));
    3. view.rootContext()->setContextProperty("stopwatch", new Stopwatch);
    4. view.show();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. import Qt 4.7
    2.  
    3. Rectangle {
    4. width: 300
    5. height: 300
    6.  
    7. MouseArea {
    8. anchors.fill: parent
    9. onClicked: {
    10. if (stopwatch.isRunning())
    11. stopwatch.stop()
    12. else
    13. stopwatch.start();
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    this would give me an error: TypeError: Result of expression 'stopwatch.start' [undefined] is not a function.

    Also one example of connecting signal/slot:

    Qt Code:
    1. QDeclarativeView view;
    2. view.setSource(QUrl::fromLocalFile("main.qml"));
    3. QObject *rootObject = dynamic_cast<QObject*>(view.rootObject());
    4. QObject::connect(rootObject, SIGNAL(someqmlfunc()), &this, SLOT(somecpluplusfunc()));
    To copy to clipboard, switch view to plain text mode 

    but conversion from QGraphicsObject to QObject is not possible.

    Is there some working way to call a c++ function from within qml at the moment?

  2. #2
    Join Date
    Sep 2010
    Posts
    14
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QML and C++ interaction

    Ah, forgot to mark the function I wish to run in C++ with Q_INVOKABLE. Now works.

  3. #3
    Join Date
    Oct 2009
    Posts
    47
    Thanks
    10

    Default Re: QML and C++ interaction

    Hi,

    I'm having the same issue, can you post how you got it working? the C++ function you just have to surround with Q_+INVOKABLE?

    WHat if you want to send a SIGNAL from QML that has data the user entered (from QML). to a C++ slot that will process that data.

  4. #4
    Join Date
    Sep 2010
    Posts
    14
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QML and C++ interaction

    Haven't yet needed a signal slot mechanism between QML and Qt but here's how you can call C++ code from QML:

    First you need your class:

    Qt Code:
    1. #include <QObject>
    2.  
    3. class MyClass : public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit MyClass(QObject *parent = 0);
    8.  
    9. Q_INVOKABLE void doThisAndThat(QString myString);
    10. Q_INVOKABLE QString getThisAndThat();
    11.  
    12. signals:
    13.  
    14. public slots:
    15.  
    16. private:
    17.  
    18. QString localThisAndThat;
    19. };
    To copy to clipboard, switch view to plain text mode 

    And you can create the implementation as you wish, no need to use the Q_INVOKABLE on the implementation side, though.

    The you need to introduce this class to QML like this:

    Qt Code:
    1. QDeclarativeView::rootContext()->setContextProperty("qmlReferenceName", myClass);
    To copy to clipboard, switch view to plain text mode 

    So you just give a string of your choice to qmlReferenceName, and pass your class on myClass. After this, you have this class as a global scope variable within your QML code:

    Qt Code:
    1. Rectangle {
    2. MouseArea {
    3. anchors.fill: parent
    4. onClicked: { qmlReferenceName.doThisAndThat("It works!") }
    5. }
    6.  
    7. Text {
    8. text: qmlReferenceName.getThisAndThat()
    9.  
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QProcess and Interaction Between 2 QT Apps
    By sam_1802 in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2010, 10:08
  2. UI Interaction gone!
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 15th May 2010, 12:08
  3. QML interaction with C++ (not only signals & slots)
    By Palmik in forum Qt Programming
    Replies: 1
    Last Post: 1st February 2010, 20:32
  4. Qt interaction with X11
    By qwakaw in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 20:17
  5. Replies: 1
    Last Post: 5th August 2008, 19:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.