View Full Version : Signal from Qt to Qml function not working when using parameters...

7th April 2011, 17:22

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

//! [imports]
import QtQuick 1.0
import "content"
//! [imports]

//! [0]
Rectangle {
color: "#545454"
width: 300; height: 300

// Signals
signal sliderX(int X)

// Slots
function settemp(){ dial.value = 50; }
function setslider(value){ dial.value = value; }

Dial {
id: dial
anchors.centerIn: parent
value: 10
//! [0]

#ifndef MYTEST_H
#define MYTEST_H

#include <QObject>

class myTest: public QObject

myTest(QObject *parent) : QObject(parent)
m_value = 0;
m_parent = parent;
connect(m_parent, SIGNAL(sliderX(int)), this, SLOT(setValue(int)));

connect(this, SIGNAL(temp()), m_parent, SLOT(settemp()));
connect(this, SIGNAL(valueChanged(int)), m_parent, SLOT(setslider(int)));

emit temp();
emit valueChanged(100);
int value() const { return m_value; }

public slots:
void setValue(int value)
if (value != m_value)
m_value = value;
emit valueChanged(value);
void valueChanged(int newValue);
void temp();

int m_value;
QObject *m_parent;

#endif // MYTEST_H

#include <QApplication>
#include <QWidget>
#include <QtGui>
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeContext>
#include "myTest.h"

int main(int argc, char *argv[])
QApplication a(argc, argv);

QDeclarativeView view;


QObject *object = view.rootObject();

QDeclarativeContext *context = view.rootContext();

myTest test(object);

(*context).setContextProperty("myTest", &test);


return a.exec();

7th April 2011, 21:05
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).

8th April 2011, 11:00
Seems to work nicely, thanks.

If it would help anyone else in the future I changed these lines:

#include <QVariant>
connect(this, SIGNAL(valueChanged(QVariant)), m_parent, SLOT(setslider(QVariant)));
void valueChanged(QVariant newValue);

8th February 2012, 13:56
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:)

22nd January 2014, 02:25
That's the point.