I made sure: Your code is fine. Works for me:
signalslot.pro:
QT += core
TARGET = signalslot
TEMPLATE = app
HEADERS += Counter.h
SOURCES += main.cpp
counter.h:
#ifndef COUNTER_H
#define COUNTER_H
#include <QObject>
#include <QDebug>
{ Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
qDebug() << "ValueChanged: " << value;
}
}
signals:
void valueChanged(int newValue);
private:
int m_value;
};
#endif // COUNTER_H
main.cpp:
#include <QApplication>
#include "Counter.h"
#include <QDebug>
int main(int argc, char *argv[])
{
Counter a, b;
QObject::connect(&a,
SIGNAL(valueChanged
(int)),
&b,
SLOT(setValue
(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
return 0;//a.exec();
}
signalslot.pro:
QT += core
TARGET = signalslot
TEMPLATE = app
HEADERS += Counter.h
SOURCES += main.cpp
counter.h:
#ifndef COUNTER_H
#define COUNTER_H
#include <QObject>
#include <QDebug>
class Counter : public QObject
{ Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
qDebug() << "ValueChanged: " << value;
}
}
signals:
void valueChanged(int newValue);
private:
int m_value;
};
#endif // COUNTER_H
main.cpp:
#include <QApplication>
#include "Counter.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),&b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
return 0;//a.exec();
}
To copy to clipboard, switch view to plain text mode
Output:
ValueChanged: 12
ValueChanged: 12
ValueChanged: 48
Try deleting the makefiles. clean the project.
What environment are you using? QtCreator?
Johannes
Bookmarks