Results 1 to 3 of 3

Thread: Connecting QML to a C++ signal - connection doesn't work on the first time

  1. #1
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Connecting QML to a C++ signal - connection doesn't work on the first time

    I'm playing around with connections between C++ and QML.

    I've found a case in which it seems like a signal connection doesn't work at first, then works on calls from other locations in the code.

    A bit of code to clarify:
    Qt Code:
    1. // Header
    2. #include <QObject>
    3. #include <QDateTime>
    4.  
    5. class MyQmlInterface : public QObject
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10.  
    11. Q_PROPERTY(QString singleValue READ singleValue WRITE setSingleValue NOTIFY singleValueChanged)
    12.  
    13. const QString &singleValue() const;
    14. void setSingleValue(const QString &newSingleValue);
    15. void emitCppSignal(const QString &passedStr);
    16.  
    17. Q_INVOKABLE QDateTime getCurrentDateTime() const {
    18. return QDateTime::currentDateTime();
    19. }
    20.  
    21. signals:
    22. void singleValueChanged();
    23. // THIS is the signal I want to connect to
    24. void signalFromCpp(const QString &passedStr);
    25.  
    26. public slots:
    27. void somethingHappened();
    28.  
    29. private:
    30. QString mSingleValue;
    31. };
    32.  
    33. // Cpp
    34.  
    35. const QString &MyQmlInterface::singleValue() const
    36. {
    37. return mSingleValue;
    38. }
    39.  
    40. void MyQmlInterface::setSingleValue(const QString &newSingleValue)
    41. {
    42. if (newSingleValue == mSingleValue)
    43. return;
    44. mSingleValue = newSingleValue;
    45. emit singleValueChanged();
    46. }
    47.  
    48. void MyQmlInterface::somethingHappened()
    49. {
    50. setSingleValue("O RLY");
    51. }
    52.  
    53. // Method to emit the signal
    54. void MyQmlInterface::emitCppSignal(const QString &passedStr)
    55. {
    56. emit signalFromCpp(passedStr);
    57. }
    58.  
    59. // QML
    60.  
    61. import QtQuick 1.0
    62.  
    63. Rectangle {
    64. width: 360
    65. height: 360
    66.  
    67. Text {
    68. id: myText
    69. anchors.centerIn: parent
    70. text: myInterface.singleValue
    71. }
    72.  
    73. Connections {
    74. target: myInterface
    75. onSignalFromCpp: {
    76. myText.text += passedStr
    77. myText.rotation += 90
    78. }
    79. }
    80. }
    To copy to clipboard, switch view to plain text mode 

    Now here is where I initialize my Qml display and expect to see the result of my signal being emitted:
    Qt Code:
    1. // The types are what you expect :)
    2. mView = new QDeclarativeView(this);
    3. mEngine = mView->engine();
    4. mContext = mEngine->rootContext();
    5.  
    6. mQmlInterface = new MyQmlInterface;
    7.  
    8. QString filePath = "D:\\QmlTest\\QmlTest.qml";
    9. mView->setSource(filePath);
    10.  
    11. mContext->setContextProperty("myInterface", mQmlInterface);
    12. mQmlInterface->setSingleValue("Set after context...");
    13.  
    14. ui.verticalLayout->addWidget(mView);
    15. mView->show();
    16.  
    17. //! If needed - this is how to clean up in case Qml wants to quit
    18. //connect(mEngine, SIGNAL(quit()), this, SLOT(declarativeEngineQuit()));
    19.  
    20. // This first signal call doesn't work
    21. mQmlInterface->emitCppSignal("this getting to you?");
    To copy to clipboard, switch view to plain text mode 

    The above code runs when I click on a button to "start" the qml test. In a different place in my code (index change of a table widget) I call the same method.
    Qt Code:
    1. mQmlInterface->emitCppSignal("this getting to you?");
    To copy to clipboard, switch view to plain text mode 

    Current behavior is that only the index change calls are getting through the QML. For my implementation I can live with that but why is it happening? Is this a bug?

  2. #2
    Join Date
    Oct 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connecting QML to a C++ signal - connection doesn't work on the first time

    You have to register your class to qdeclarative engine to be able to connect to C++ object signals which are not notify signals of object properties. Registration can be done using qmlRegisterType template function:
    Qt Code:
    1. template<typename T>
    2. int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
    To copy to clipboard, switch view to plain text mode 
    For you code it would be for example:
    Qt Code:
    1. qmlRegisterType<MyQmlInterface>("MyQmlInterfaceNamespace", 1, 0, "MyQmlInterface");
    To copy to clipboard, switch view to plain text mode 
    More info:
    http://doc.qt.nokia.com/4.7/qdeclara...mlRegisterType
    http://doc.qt.nokia.com/4.7/qml-extending.html

  3. #3
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connecting QML to a C++ signal - connection doesn't work on the first time

    Thanks for your reply.

    The code given here should still work. And as matter of fact, it does, for any later call to emitCppSignal. That's why I wrote, it doesn't work only for the first call.
    I changed some things in my actual project so I don't have this first call in my working code, but if I manage to find time to repeat it I'll try to post a better explanation of the issue.

Similar Threads

  1. Replies: 1
    Last Post: 14th March 2011, 10:21
  2. Replies: 1
    Last Post: 15th November 2010, 11:36
  3. Replies: 0
    Last Post: 12th August 2010, 16:05
  4. signal of QListWidget doesn't work
    By vito49 in forum Qt Programming
    Replies: 10
    Last Post: 1st October 2008, 05:07
  5. Connection doesn't work
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 20th March 2007, 12:09

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.