PDA

View Full Version : emit signal and recognizing in a different class



salmanmanekia
17th October 2011, 13:50
Hi,
I am emitting a signal from A class and anticipating that the connect statemt in the B class connects my signal from A class to the slot function in the B class..
Two ways i understood for this !..
1, If i put an #include A statement in the B,
and/or
2, If B inherits from A,
Please ,if any one can point out which is the right approach or if both are dumb :P and i shud do something else..
Thanks already ...

Zlatomir
17th October 2011, 14:31
You don't need to derive or create the objects one into the other, you can also create the objects in the same scope, like:


int main()
{
//...
A objA;
B objB;
QObject::connect(&objA, SIGNAL(boo()), &objB, SLOT(onBooDoSomething()));
//...
}

Basically you just need to pointers to those objects (and of course you need to make sure that the object "live" in the same time ;) )
Read more here (http://doc.qt.nokia.com/stable/signalsandslots.html)

salmanmanekia
17th October 2011, 14:44
it seems that my problem statement wasnt accurate to be properly understood.... i will repeat my question again
i have two classes for eg A and B ..


#include "B"
class A: public QObject
{
..
B bObj;
..
connect (&bObj,SIGNAL(bSignal()), this, SLOT(anyAFunc()));
..
}
------------------------------------------------------------------------
class b: public QWIdget
{
..
emit bSignal();
..
..
}

Now this techque seems to be a flaw as the compiler complains

Object::connect: No such signal b::bSignal()

Zlatomir
17th October 2011, 14:51
Didn't you forgot to declare bSignal with signals: access specifier?

//and... as i said in previous post you don't have to use composition (or inheritance) for signal-slot connection to work.

salmanmanekia
17th October 2011, 15:36
the bSignal is declared with the signals access specifier....

FelixB
17th October 2011, 15:42
please show the header with the definition of B

bruceariggs
17th October 2011, 20:53
#include <QWidget>

class b: public QWidget
{

Q_OBJECT

public:

// Public functions here

public slots:

// Public slots here

signals:

void bSignal(); // <-- This is how you declare a signal

protected:

// Protected members here

private:

// Private members here
};

salmanmanekia
18th October 2011, 07:42
B.hpp


#include <QWidget>
#include <QGridLayout>
#include <QLabel>
#include <QToolButton>
#include <QIcon>
#include <QComboBox>

namespace UpstreamPrivateNamespace{

class B : public QWidget
{
Q_OBJECT

public:
B( QWidget * ParentP = 0 );

signals:
.....
void bSIgnal(const QString&);

public slots:

void bSlot(const QString& );


private:
QIcon * iconVar;
QComboBox * boxVar;
};
}


B.cpp


b::b( QWidget * ParentP )
: QWidget( ParentP )
{
..
this->boxVar = new QComboBox(this);
connect(this->boxVar, SIGNAL(currentIndexChanged(const QString& )), this, SLOT(bSlot(const QString& )));
..
}
void b::bSlot(const QString& var)
{

emit bSignal(var);

}

FelixB
18th October 2011, 07:49
does bSlot get called when you change the combobox index?

btw, you can connect signals with signals directly:


connect(this->boxVar, SIGNAL(currentIndexChanged(const QString& )), this, SIGNAL(bSignal(const QString& )));

salmanmanekia
18th October 2011, 08:13
does bSlot get called when you change the combobox index?
yes


btw, you can connect signals with signals directly:
i remember this now, this is what happens when u do programming after 3 years gap .. :)

I will also mention the A class ..

A.hpp

..
public slots:
void setDev(const QString &);
private:
B * bObj ;
..

A.cpp


constructor
bObj = new b();
connect(bObj, SIGNAL(bSignal(const QString &)), this, SLOT(setDev(const QString &)));
// constructor ends here

void setDev(const QString & dev)
{
QLOG_INFO() << dev ;
}

The const was missing from the connect statemet in the Signal ... the 'No such signal ' warning disapears but the connect statement doesnt work ...as the A:setDev function isnt called when the combobox value changes ....

boudie
18th October 2011, 08:17
Check your spelling.

void bSIgnal(const QString&);
!=
void bSignal(const QString&);

salmanmanekia
18th October 2011, 08:19
Thanks boudie its a typo in the forum .....in the code the spellings are correct !