PDA

View Full Version : Signals and Slots - why won't this constellation not work



FlashMuller
10th June 2010, 14:54
Hey guys,
I've been working with Qt for a few weeks now, still I can't get this specific Signal/Slot/Constellation running.
I've got a class, "Analytic", which is a QObject (defined in Analytics.h) and has got a SLOT called display(), which is called at the end of another function ( in Analytics.cpp).
Now I've got a Mainwindow (defined in MWindow.h) which has got the public SLOT draw_layer() (defined in MWindow.cpp).
What I additionally do is

Analytics *Analy = new Analytics();
connect(Analy, SIGNAL(display()), this, SLOT(draw_layer()));

this is both happening in the main function of my MWindow.cpp

To sum it up: I've got 4 files -> 2 cpp and 2 h. In Header/File 1 I've got a signal, and in Header/File 2 I've got the public SLOT. Qt doesn't give me an error when compiling or while running, but the SLOT is just not triggered, although it works as a function.
Thanks in advance,
Daniel

Lykurg
10th June 2010, 15:00
Show us your code! One guess: did you use the Q_OBJECT macro and did you emit the signal properly?

CeeKey
10th June 2010, 15:03
Hey,

your display() Slot has to be a Signal. This Signal musst be emitted if your Event is processed.

amoswood
10th June 2010, 15:09
Yeah, I would check that display() slot is defined under as a signal and that you include the Q_OBJECT macro in your class definition and that you emit your signal. (See below)


class Analyic : public QObject
{
Q_OBJECT
public:
Analytic();
void process_something()
{
// process something here
emit signalDisplay();
}
signals:
void signalDisplay();
}

Zlatomir
10th June 2010, 15:10
Most likely you forgot to emit the display() signal... or if you emit it, check your call/connection to the function/slot in witch you emit the signal.

FlashMuller
10th June 2010, 15:12
That was display()-thing was a typo. It is a SIGNAL


Here is my code. I'll just post the relevant parts, but you'll habe to live with the fact, that the naming of my files/variables is german:

DGM_Analyse.h:


#ifndef DGM_ANALYSE
#define DGM_ANALYSE
#include "Mainheader.h"

class DGM_Analytik:public QObject
{
Q_OBJECT
public:
DGM_Analytik(QObject* = 0);

signals:
void entw_anzeigen();

};
#endif

---------------------------------
DGM_Analyse.cpp:
Well, a lot of functions in here, so the on line about the signal:



emit entw_anzeigen();

----------------------------------
Hauptfenster.h: (MainWindow)


#ifndef HAUPTFENSTER_H
#define HAUPTFENSTER_H

#include "DGM_Analyse.h"



class Hauptfenster:public QMainWindow
{
Q_OBJECT

public:
Hauptfenster();


public slots:
void Entw_Layer();
};
#endif

-----------------------------------------
and finally Hauptfenster.cpp:


Hauptfenster::Hauptfenster()
{

...
Analytik = new DGM_Analytik(this);
connect(Analytik, SIGNAL(entw_anzeigen()), this, SLOT(Entw_Layer()));
...

}

Hope that helped.
Daniel

CeeKey
10th June 2010, 15:16
You still have to emit your signal!

FlashMuller
10th June 2010, 15:22
Didn't I do so by
"emit entw_anzeigen();"?
in the DGM_Analyse.cpp?

Zlatomir
10th June 2010, 15:23
In file DGM_Analyse.cpp, the emit entw_anzeigen(); is within a function or slot (most likely, because you probably need to display() when the user does something... ) so check if the function is called or that slot gets connected (with button click or whatever user action it should be connected with).

CeeKey
10th June 2010, 15:30
Didn't I do so by
"emit entw_anzeigen();"?
in the DGM_Analyse.cpp?

Yes, but i can't see in which case you emit this signal. So it looks like you did not call this in the case you would like.

FlashMuller
10th June 2010, 15:38
Zlatomir is right,
the emit command is inside a function which is a slot itself. Anyway, this works: The Slot-function is being called, doing its job and then emitting the signal. I did a Step-by-step Debug and when reaching the "emit-command" first the moc-file is being opened

void DGM_Analytik::entw_anzeigen()
{
QMetaObject::activate(this, &staticMetaObject, 1, 0);
}

and then Qt jumps through the QObject.cpp file. It breaks at this point:


if (!sender->d_func()->isSignalConnected(signal_index))
return; // nothing connected to these signals, and no spy

which of course tells me that the connection doesn't seem to work, but doesn't really tell me how to fix it.

amoswood
10th June 2010, 15:53
In the debug console window, it should state if the connection was made or give an error when the connect() function is called.

CeeKey
10th June 2010, 15:54
It looks right for me, so you have to post your whole code, or we can only guess along.

FlashMuller
10th June 2010, 15:57
It should :-)
It doesn't give an error while debugging (QObject: No such Slot or Signal) and it doesn't give any feedback when it returns from the QObject.cpp.
Daniel

FlashMuller
10th June 2010, 16:04
In the attachment you will find the four files. You don't have a working programm with that, but you can try to catch up with me like that. Hope the variable-names aren't a too big problem.
Thanks

P.S.: Please no comments about the rest of my programming ;-)

FlashMuller
13th June 2010, 10:04
Noone got an idea? (in other ords: PUSH :p )