[SOLVED] Issues with SIGNALS and SLOTS
-
Hi All,
I am trying something with SIGNALS and SLOTS:
Two classes defined:
-
WeightTracker
-
Display
Header files given below
WeightTracket.h
@
#include <QObject>
#include <QDate>class weightTracket : public QObject
{
Q_OBJECT
public:
explicit weightTracket(QObject *parent = 0);
void update (QDate d, float w);QDate getDate(); float getFloat();
signals:
void changed();
public slots:
private:
QDate date; float weight;
@
display.h
@
class display : public QObject
{
Q_OBJECT
public:
explicit display(QObject *parent = 0);signals:
public slots:
void show(weightTracket * t);
};
@
The "show" method in display class
@
void display::show(weightTracket * t){qDebug() << t->getDate().toString() << t->getFloat();
}
@
Main:
@
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);weightTracket * tracker = new weightTracket(); QDate d1(2014,04,14); tracker->update(d1,123); display * output = new display(); QObject::connect(tracker,SIGNAL(changed()),output,SLOT(show(tracker))); return a.exec();
}
@
When I try to run the program I get the message:
QObject::connect: No such slot display::show(tracker)
is the output.
Where am I going wrong?
-
-
I think its is incompatibility of parameters at the connection. Normally, you have to emit at SIGNAL function the same parameters that you are going to use in SLOT function.
Since the SIGNAL function changed() has no parameters then the SLOT function shouldn't have parameters.
-
Dont think that is the answer. Signals seldom have parameters. I think.
Anyway how do I link the classes?
-
I updated the show() method for testing as below:
@
void display::show(){//qDebug() << t->getDate().toString() << t->getFloat(); qDebug() << "In the slot";
}
@
I changed the connection to :
@
QObject::connect(tracker,SIGNAL(changed()),output,SLOT(show()));@
The message is gone but I am not getting the qDebug() message from the display() slot.
-
Hi,
It's normal, show() is a non virtual public method from QWidget, so yours will not be called.
Many signals throughout the Qt library have parameters.
See the "Signal and Slot":http://qt-project.org/doc/qt-5/signalsandslots.html chapter, it's explained nicely with examples
-
I have changed the name to show3(). But still does not work.
Thanks SGaist for the link. Really helpful.
My connection is still not working.
-
Do you emit changed anywhere in your code ? If so and it happens before the connect statement then it won't have any effect.
-
On the dot. The connection was after the signal was emitted. Changed it now it works.
Thanks. As usual you are a savior.
In one class I have a QDate and float that is updated. In the second class (display class) I need to display it to the console.
What is the best way to pass that information using SIGNALS and SLOTs?
-
Got it done.
I am making the signal emit the QDate and float and the slot is consuming the QDate and float.
Thank you once again.