Results 1 to 5 of 5

Thread: Signal from Qt to Qml function not working when using parameters...

  1. #1
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt4

    Default Signal from Qt to Qml function not working when using parameters...

    Hi,

    Been searching around for an explanation to this problem, but no luck. Any help appreciated

    I want to send a signal from my c++ class to a qml function/slot

    If I do it without using any parameters it works nicely, like this:
    connect(this, SIGNAL(temp()), m_parent, SLOT(settemp()));

    But when adding parameters of type int it fails with the "No such slot" error, like this:
    connect(this, SIGNAL(valueChanged(int)), m_parent, SLOT(setslider(int)));

    Please study the code below and help a Qt newb ...

    Qt Code:
    1. //! [imports]
    2. import QtQuick 1.0
    3. import "content"
    4. //! [imports]
    5.  
    6. //! [0]
    7. Rectangle {
    8. color: "#545454"
    9. width: 300; height: 300
    10.  
    11. // Signals
    12. signal sliderX(int X)
    13.  
    14. // Slots
    15. function settemp(){ dial.value = 50; }
    16. function setslider(value){ dial.value = value; }
    17.  
    18. Dial {
    19. id: dial
    20. anchors.centerIn: parent
    21. value: 10
    22. }
    23. }
    24. //! [0]
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MYTEST_H
    2. #define MYTEST_H
    3.  
    4. #include <QObject>
    5.  
    6. class myTest: public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. myTest(QObject *parent) : QObject(parent)
    12. {
    13. m_value = 0;
    14. m_parent = parent;
    15. connect(m_parent, SIGNAL(sliderX(int)), this, SLOT(setValue(int)));
    16.  
    17. connect(this, SIGNAL(temp()), m_parent, SLOT(settemp()));
    18. connect(this, SIGNAL(valueChanged(int)), m_parent, SLOT(setslider(int)));
    19.  
    20. emit temp();
    21. emit valueChanged(100);
    22. }
    23. int value() const { return m_value; }
    24.  
    25. public slots:
    26. void setValue(int value)
    27. {
    28. if (value != m_value)
    29. {
    30. m_value = value;
    31. emit valueChanged(value);
    32. }
    33. }
    34. signals:
    35. void valueChanged(int newValue);
    36. void temp();
    37.  
    38. private:
    39. int m_value;
    40. QObject *m_parent;
    41. };
    42.  
    43. #endif // MYTEST_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QtGui>
    4. #include <QtDeclarative/QDeclarativeView>
    5. #include <QtDeclarative/QDeclarativeContext>
    6. #include "myTest.h"
    7.  
    8.  
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication a(argc, argv);
    13.  
    14. QDeclarativeView view;
    15.  
    16. view.setSource(QUrl::fromLocalFile("../test2/qml/dialcontrol.qml"));
    17.  
    18. QObject *object = view.rootObject();
    19.  
    20. QDeclarativeContext *context = view.rootContext();
    21.  
    22. myTest test(object);
    23.  
    24. (*context).setContextProperty("myTest", &test);
    25.  
    26. view.show();
    27.  
    28. return a.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Signal from Qt to Qml function not working when using parameters...

    Take a look on setslider slot method! Does parameter have a type? No since it is java script. So what kind of parameter type should be in C++ code? int? I don't think so. I would try to use QVaraint as slot parameter type (and correct signal too).
    Last edited by MarekR22; 7th April 2011 at 20:11.

  3. The following user says thank you to MarekR22 for this useful post:

    Palooka (8th April 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt4

    Default Re: Signal from Qt to Qml function not working when using parameters...

    Seems to work nicely, thanks.

    If it would help anyone else in the future I changed these lines:
    Qt Code:
    1. #include <QVariant>
    2. ...
    3. connect(this, SIGNAL(valueChanged(QVariant)), m_parent, SLOT(setslider(QVariant)));
    4. ...
    5. signals:
    6. void valueChanged(QVariant newValue);
    7. ...
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Dec 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Signal from Qt to Qml function not working when using parameters...

    Hello Palooka,

    Can u please explain the code? I mean about the QDeclarativeContext and the pointer variables?
    How exactly the functioning is happennig?
    thanx in advance

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

    Default Re: Signal from Qt to Qml function not working when using parameters...

    That's the point.

Similar Threads

  1. Replies: 4
    Last Post: 20th January 2011, 01:03
  2. Signal and Slots with Large Parameters
    By metdos in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2010, 15:48
  3. QScript + get function call parameters
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2009, 15:30
  4. Wrong function according to parameters is taken
    By Lykurg in forum General Programming
    Replies: 5
    Last Post: 20th March 2009, 19:55
  5. How many copies of Signal parameters
    By Doug Broadwell in forum Newbie
    Replies: 3
    Last Post: 2nd December 2006, 21:34

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.