PDA

View Full Version : Custom signal



JovianGhost
17th March 2010, 00:52
I've got a context menu whose triggered() signal I've connected to another widget's slot, and this works fine.
But I want to send some more information along with this, so I decided I need a custom signal. I read the docs, and I figure I need to do something like the following:

- Connect the triggered() signal of the menu item to a local slot
- Connect the custom signal of the class to wherever I need it to go
- In the local slot, emit my custom signal

So my code would look like this:


// Class declaration
class Foo : public QObject
{
Q_OBJECT

....

signals:
void MySignal(int someData);

private slots:
void MyLocalSlot();
}

...

// Somewhere in the local class
connect(action, SIGNAL(triggered()), this, SLOT(MyLocalSlot()));
connect(this, SIGNAL(MySignal(int), remoteObject, SLOT(OnMySignal(int)));

// Local slot
void Foo::MyLocalSlot()
{
emit MySignal(5);
}


// Destination class
void DestinationClass::OnMySignal(int someData)
{
...
}

This works just fine, but I'm wondering if I'm overcomplicating things, or this is the simplest way to do it?

yogeshgokul
17th March 2010, 04:47
This works just fine, but I'm wondering if I'm overcomplicating things, or this is the simplest way to do it?
If its working fine, and you know what you have done, then who cares ;)

aamer4yu
17th March 2010, 04:48
You could have used
QSignalMapper
Although your code also seems ok.

JovianGhost
17th March 2010, 12:10
Good to know, thanks!