Results 1 to 4 of 4

Thread: QML and C++ interaction

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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
  •  
Qt is a trademark of The Qt Company.