PDA

View Full Version : Handling signal-slot



sudhansu
30th January 2010, 06:22
Hi All
I've a doubt on signal and slot mechanisim. E.g


class A
{
signals:
void sig();
}

class B
{
public:
A objOfA;
}


class C
{
B objOfB;
C();
public slots:
void slot();
}

C::C()
{
connect(&objOfB.objOfA,SIGNAL(sig(),this SLOT(slot()); /// Can we handle like this???
}



My doubt is can we handle the signal as above?

Thank you all.

faldzip
30th January 2010, 09:04
If you add missing parentheses to connect() statement and make your classes derived from QObject then this should work.

sudhansu
30th January 2010, 09:22
If you add missing parentheses to connect() statement and make your classes derived from QObject then this should work.

Hi,
By mistake i forgot to add paranthesis,. And my class is derived from QDialog,(and QDialog is derived fromQWidget->QObject), SO i think no need to directly derived it from QObject.
But still its not working?. :(

franz
30th January 2010, 10:57
Eh, what exactly isn't working?

faldzip
30th January 2010, 11:00
Can you explain what does it mean "not working"? Any warnings on console output? Can you show exact code? Or prepare minimal compilable example reproducing your problem (but do not give us your whole project, but write something like in first post but make compilable).

sudhansu
30th January 2010, 11:27
Can you explain what does it mean "not working"? Any warnings on console output? Can you show exact code? Or prepare minimal compilable example reproducing your problem (but do not give us your whole project, but write something like in first post but make compilable).

Below i'm posting the code



class CMainWindow : public QDialog, public Ui::Dialog
{
Q_OBJECT

MessageBox MessageBoxObj;

public:
CMainWindow();


public slots:
void HandelMessage(QString i_strMessage);
}
CMainWindow()::CMainWindow()
{
connect(&(MessageBoxObj.networklayerObj),SIGNAL(MessageFrom NetworkLayer(QSTring )),this,SLOT( HandelMessage(QString)));
}

class MessageBox : public QDialog, public Ui::MessageDialog
{
Q_OBJECT

public:
Network networklayerObj;
}

class Network:public QObject
{
O_OBJECT

signals:
void MessageFromNetworkLayer(QSTring message);
}

Please ignore the spelling mistakes.

faldzip
30th January 2010, 11:37
Ok, so:
1. What is not working?
2. Can check if there is any warning on console output?

sudhansu
30th January 2010, 11:41
Ok, so:
1. What is not working?
2. Can check if there is any warning on console output?

While debugging, i found that signal is emiting with some data, but its not connected to slot.
Debug statment inside slot also not priting, if i run the application.

faldzip
30th January 2010, 11:55
Don't you have any warning on console output like:
WARNING: no matching slot ...
?

For me this is working:


#include <QtCore>

class A : public QObject
{
Q_OBJECT
public:
A() : QObject(0), eventno(0) {
startTimer(2000);
}
protected:
void timerEvent(QTimerEvent *) {
if (++eventno == 2)
QCoreApplication::quit();
else
emit message("Some useful message!");
}
private:
int eventno;
signals:
void message(const QString &str);
};

class B : public QObject
{
Q_OBJECT
public:
B() : QObject(0) { }
A aObj;
};

class C : public QObject
{
Q_OBJECT
public:
C() : QObject(0) {
connect(&(bObj.aObj), SIGNAL(message(QString)), this, SLOT(showMessage(QString)));
}
public slots:
void showMessage(const QString &str) {
qDebug(str.toLocal8Bit().constData());
}
private:
B bObj;
};

int main(int argc, char **argv)
{
QCoreApplication a(argc, argv);
C cObj;
return a.exec();
}

#include "main.moc"

lyuts
31st January 2010, 10:32
I 'm not sure whether you typed your code or just copied-pasted, but your class definition has to end with "};".