Solved Sending static signals, is it possible?
-
Is it possible to send a static signal?
I have a list of entry widgets inside a list and I want the container to be notified if one of them gets selected.
I don't want to connect all the entries idividually so I tought sending a static signal might workpublic signals: static void SelectedStateChanged();
I connect it to the QCheckBox
connect(selectedCheckBox, &QCheckBox::stateChanged, [this](){ emit SelectedStateChanged(); });
Then in the list widget I connect to the static signal.
I have tried multiple ways:- Qt5 syntax:
// Connect the static signal from a dummy entry DeckScrollListEntry *dummyEntry = new DeckScrollListEntry(this); connect(dummyEntry, &DeckScrollListEntry::SelectedStateChanged, this, &DeckScrollList::refreshSelections); delete dummyEntry;
Compiler error:
error: no matching function for call to 'DeckScrollList::connect(DeckScrollListEntry*&, void (*)(), DeckScrollList*, void (DeckScrollList::*)())' this, &DeckScrollList::refreshSelections);
- Macro syntax:
DeckScrollListEntry *dummyEntry = new DeckScrollListEntry(this); connect(dummyEntry, SIGNAL(SelectedStateChanged()), this, SLOT(refreshSelections)); delete dummyEntry;
Compiler error:
error: 'this' is unavailable for static member functions QMetaObject::activate(this, &staticMetaObject, 1, nullptr); ^
Is there any way or should I redesign and make connections for every entry.
-
Connections always relay on concrete object instances. It makes no sense for signal to be static, because signal has to come from a "living" object.
You are creating the DeckScrollListEntry objects somewhere, so connect the signals there, too. Alternatively, use automated connections: connectSlotsByName(). I don't recommend it, though, because it is very easy to make a mistake here. The new connection syntax is definitely the best.
-
I'm not sure if you understood what I was trying to do. It wasn't really clear from what I posted.
Tough I have since realized that it's a horrible design that would lead to all kinds of bugs down the road.
What I was trying to do is something like thisconnect(static signal &DeckScrollListEntry::SelectedStateChanged, this, &DeckScrollList::refreshSelections) )
So it would connect to a static signal that is sent by any DeckScrollListEntry.
So in a way it would connect to every DeckScrollListEntry.
And I also realized this is not possible of course, and it would be horrible if it were possible. -
@plaktos said in Sending static signals, is it possible?:
So it would connect to a static signal that is sent by any DeckScrollListEntry.
I understood it exactly like that. It's not possible to do.
-
Disclaimer
I'm not saying this is something you'd want to do
You can use a static/singleton QObject as the source of your signal