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
{
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)
{
...
}
// 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)
{
...
}
To copy to clipboard, switch view to plain text mode
This works just fine, but I'm wondering if I'm overcomplicating things, or this is the simplest way to do it?
Bookmarks