Unsolved How to pass SIGNAL parameter(s) to SLOT as SAME parameters ?
-
Could somebody please hep me to understand how it physically works ?
I do not need another explanation about how to use or not to use "go to slot " or connect or lambda....
I do not understand how SIGNAL acquires these parameters and how they are passed to SLOT.
When I posted my prevision "go to slot" question I got confused with usage of parameters because they were added to SLOT , not to SIGNAL.Now I am asking the opposite -
how does SIGNAL process / acquire the parameters,
( could I have a debug somewhere to actually read the SIGNAL and not just indirectly tracking the list changes via dummy SIGNAL / SLOT as I am doing now ?)
and HOW to pass them to slot .can I envision these parameters as some kind of global variable ?
My attached test code detects the changes in list OK, now I need to pass the actual item to another object. ( some code would be nice, but not necessary )
connect(ui->list, SIGNAL(currentItemChanged(QListWidgetItem * current, QListWidgetItem * previous)), this, // temp same object SLOT(testSlot()) // dummy tracking changes only need to pass QListWidgetItem * current, QListWidgetItem * previous)), here );
-
SIGNAL(currentItemChanged(QListWidgetItem * current, QListWidgetItem * previous))
This code is not valid. You don't pass parameter names to SIGNAL macro, only the types. It should be
SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*))
how does SIGNAL process (...) parameters
SIGNAL
macro just stringifies the function name and parameters, so if you have avoid something(int)
signal it creates a string in a unified format like"2something(int)"
(just an example, the actual format of that string is internal and not important to you). Same goes for theSLOT
macro. Internally connect uses these strings to match function signatures. If, for educational reasons, you're curious what the format of that string is you can just write it out:qDebug() << SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*));
how does SIGNAL (...) acquire parameters
and HOW to pass them to slot .The code that emits that signal just passes them, so for example some function in the QListWidget will have:
void QListWidget::something() { ... QListWidgetItem* current = ...; QListWidgetItem* previous = ...; emit currentItemChanged(current, previous); ... }
Emiting a signal like
emit someSignal(42)
is conceptually something like this (in pseudo code). This is what moc generates as the function body of that signal:void someSignal(int param) { foreach (connected_slots) { if (stringified_signature_of_signal matches stringified_signature_of_slot) slot(param); } }
can I envision these parameters as some kind of global variable ?
I'm not sure what you mean. You're getting them in the slot so you can see them there. If you want to inspect them before the signal is emitted just set a breakpoint where the signal is emitted. In your example you ignore signal parameters. If you want to inspect them in the slot you need to change the slot signature to receive them:
void testSlot(QListWidgetItem * current, QListWidgetItem * previous)
and change your connect statement accordingly to use
SLOT(testSlot(QListWidgetItem*, QListWidgetItem*))