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 ...
//! [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]
//! [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]
To copy to clipboard, switch view to plain text mode
#ifndef MYTEST_H
#define MYTEST_H
#include <QObject>
{
Q_OBJECT
public:
{
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);
}
}
signals:
void valueChanged(int newValue);
void temp();
private:
int m_value;
};
#endif // MYTEST_H
#ifndef MYTEST_H
#define MYTEST_H
#include <QObject>
class myTest: public QObject
{
Q_OBJECT
public:
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);
}
}
signals:
void valueChanged(int newValue);
void temp();
private:
int m_value;
QObject *m_parent;
};
#endif // MYTEST_H
To copy to clipboard, switch view to plain text mode
#include <QApplication>
#include <QWidget>
#include <QtGui>
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeContext>
#include "myTest.h"
int main(int argc, char *argv[])
{
QDeclarativeView view;
view.
setSource(QUrl::fromLocalFile("../test2/qml/dialcontrol.qml"));
QObject *object
= view.
rootObject();
QDeclarativeContext *context = view.rootContext();
myTest test(object);
(*context).setContextProperty("myTest", &test);
view.show();
return a.exec();
}
#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;
view.setSource(QUrl::fromLocalFile("../test2/qml/dialcontrol.qml"));
QObject *object = view.rootObject();
QDeclarativeContext *context = view.rootContext();
myTest test(object);
(*context).setContextProperty("myTest", &test);
view.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks