Results 1 to 4 of 4

Thread: [SOLVED] qml signal with c++ slot

  1. #1
    Join Date
    Dec 2010
    Posts
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default [SOLVED] qml signal with c++ slot

    I tried to connect a QML-Signal with a C++-Slot, but it didn't work.
    It didn't call the C++-Slot, but the connection was successful (return value from QObject::connect).

    Here is the code:

    Qt Code:
    1. // main.cpp
    2. #include <QtDeclarative>
    3.  
    4. #include "MyClass.hpp"
    5.  
    6.  
    7. int main(int argc, char *argv[]) {
    8. QApplication app(argc, argv);
    9.  
    10. QDeclarativeView view;
    11. MyClass myClass;
    12. QObject *object = view.rootContext();
    13. QObject::connect(object, SIGNAL(mySignal()), &myClass, SLOT(cppSlot()));
    14. view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    15. view.show();
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // myclass.hpp
    2. #ifndef MYCLASS_HPP
    3. #define MYCLASS_HPP
    4.  
    5. #include <QObject>
    6. #include <QDebug>
    7.  
    8. class MyClass : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public slots:
    13. void cppSlot() {
    14. qDebug() << "Called the C++ slot with";
    15. }
    16. };
    17. #endif // MYCLASS_HPP
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // MyItem.qml
    2. import QtQuick 1.0
    3.  
    4. Item {
    5. width: 100; height: 100
    6. Rectangle {
    7. anchors.fill: parent
    8. color: "black"
    9. }
    10.  
    11. signal mySignal()
    12.  
    13. MouseArea {
    14. anchors.fill: parent
    15. onClicked: {
    16. mySignal()
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    I would be very thankful, if somebody could help me!

    thanks


    Added after 1 2 minutes:


    I solved it:

    I called the functions in the wrong order, main.cpp should look like this:

    Qt Code:
    1. int main(int argc, char *argv[]) {
    2. QApplication app(argc, argv);
    3.  
    4. QDeclarativeView view;
    5. view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    6. MyClass myClass;
    7. QObject *object = view.rootObject();
    8. QObject::connect(object, SIGNAL(mySignal()), &myClass, SLOT(cppSlot()));
    9. view.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Sunny31; 8th December 2010 at 16:52.

  2. The following 2 users say thank you to Sunny31 for this useful post:

    JandunCN (22nd January 2014), TheIndependentAquarius (10th January 2014)

  3. #2

    Default Re: [SOLVED] qml signal with c++ slot

    I had the same problem with you, so thanks for your solved post.

  4. #3
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: [SOLVED] qml signal with c++ slot

    I now encounters the same problem, thanks for you tip.


    Added after 1 56 minutes:


    I am confused now.I did it in Qt 5.2 in the way the code below shows. And the result was that connection( c++ signal to QML function ) worked well,but connection(QML signal to C++ slot) didn't work.Did I ignore something important,I only knew that there were some differences between Qt4's way achieving this and Qt5's.Any tip is appreciated,thanks in advance.Sorry for my poor English.

    // main.cpp in Qt 5.2
    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/TestInvokeMethod/main.qml"));
    Test* test = new Test() ;
    QQmlEngine engine;
    QQmlComponent component(&engine, "qml/TestInvokeMethod/main.qml");
    QObject *object = component.create();
    QObject::connect( object, SIGNAL( toCode( /*QString*/ ) ), test, SLOT( emittedByQml( /*QString*/ ) ) ); // didn't work.
    QObject::connect( test, SIGNAL( toQml( QVariant ) ), object, SLOT( emittedByCode( QVariant ) ) ) ;
    test->toQml( "toQml" ) ;
    delete object ;
    viewer.showExpanded();

    return app.exec();
    }
    Last edited by JandunCN; 22nd January 2014 at 05:22.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] qml signal with c++ slot

    SIGNAL and SLOT macros only contain function names and argument types.
    /*QString*/ is neither, it is a comment.

    Cheers,
    _

Similar Threads

  1. Signal connected to slot (or signal)
    By Althor in forum Newbie
    Replies: 2
    Last Post: 6th July 2010, 11:00
  2. Signal and slot
    By jayreddy in forum Qt Programming
    Replies: 12
    Last Post: 15th December 2009, 06:05
  3. Signal and slot
    By Sheng in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2008, 18:46
  4. Replies: 1
    Last Post: 8th November 2007, 18:11
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 19:52

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.