Solved How to "emit " SIGNAL " after "addItem"?
-
@fcarney Appreciate your clarification.
I guess typical case "you are dammed if you do or do not ".
I tried to give my question some background ( some people need ) and see how it got away from the real issue.Precise terminology is useful, but the message Is
"missing ... "and it does NOT imply "warning " to me ..... -
@AnneRanch said in How to "emit " SIGNAL " after "addItem"?:
"missing ... "and it does NOT imply "warning " to me .....
"Missing" implies "missing". That has nothing to do with whether something is an error, a warning, a informational message, a hint or a philosophical musing. On the other hand, doesn't the yellow colored triangle and text tell you it's a warning, and red color indicates an error? What is the point of you often stating you do not like the wording of messages? They are what they are.
If you care, https://www.kdab.com/nailing-13-signal-slot-mistakes-clazy-1-3/ states:
11. incorrect-emit
For readability purposes you should always use
emit
(orQ_EMIT
) when calling a signal. Conversely, you should not use those macros when calling something other than a signal.Clazy will warn if you forget to use
emit
(orQ_EMIT
) or if you use them when you shouldn’t.which I would have thought is the actually important information.
-
OK, I am still not getting it.
My syntax is obviously wrong.emit pass_item(); works emit mySignal(item); does not work
// signals:
// void mySignal(QListWidgetItem * item);connect(this, SIGNAL(mySignal(item)), this , SLOT(testSlot()));
This works fine
connect(this, SIGNAL(pass_item()), this , SLOT(testSlot_PASS_DATA() ));
And here is my debug / run time error
TASK : test local SIGNAL / SLOT connect
File : ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp
Function : testSlot
@line : 380
count : 1
QObject::connect: No such signal DeviceDiscoveryDialog::mySignal(item) in ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp:148
QObject::connect: (sender name: 'DeviceDiscovery')
QObject::connect: (receiver name: 'DeviceDiscovery')TASK : test local SIGNAL / SLOT connect
File : ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp
Function : testSlot
@line : 380
count : 2 -
@AnneRanch said in How to "emit " SIGNAL " after "addItem"?:
SIGNAL(mySignal(item)),
When connecting signals this (old)
SIGNAL
macro way, you need to specify the type(s) of the parameter(s):SIGNAL(mySignal(QListWidgetItem *))
-
@AnneRanch You should avoid using old connect syntax and use new one:
connect(this, SIGNAL(mySignal(item)), this , SLOT(testSlot()));
becommes:
connect(this, &ClassName::mySignal, this , &ClassName::testSlot);
So you will avoid this kind of errors.
-
@KroMignon
We have suggested this many, many times.....! :) -
It's the exact same problem you reported here. You can apply the same solution
-
@VRonin said in How to "emit " SIGNAL " after "addItem"?:
It's the exact same problem you reported here. You can apply the same solution
Yes, same issue.
I think if somebody said "pointer to function" it may have help.Now as far as "passing the data" the syntax looks as standard passing parameter to a function passed as a parameter.
Sounds silly but that is what it is , nothing Qt special when implementing "connect". -
@JonB said in How to "emit " SIGNAL " after "addItem"?:
@KroMignon
We have suggested this many, many times.....! :)
Thanks for keeping track
and you may expect more of the same in the future - so ignore it. -
OK, few more questions , if I am allowed to push my luck here.
The SIGNAL function has parameter "item" , then
the SLOT function should have same type of parameter?BUT the new connect syntax does not require the parameters, hence in "connect" neither SIGNAL or SLOT functions are with parameters, and it passes MOC fine.
How do I process the undeclared parameter in my SLOT function?
BUT If I declare the SLOT with parameter my code breaks.
Again if the above questions are bothersome / repetitious just ignore my post.
emit pass_item(); emit mySignal(item);
// signals:
// void mySignal(QListWidgetItem * item);
// emit testSlot_PASS_DATA ();connect(this, &DeviceDiscoveryDialog::mySignal, this , &DeviceDiscoveryDialog::testSlot); connect(this, &DeviceDiscoveryDialog::mySignal, this , &DeviceDiscoveryDialog::testSlot_PASS_DATA);
-
@AnneRanch if your slot doesn't require parameter it's ok. If you need to catch the parameter but can't provide/modify corresponding slot for use with parameter, you can still catch it using lambda (using new connect() syntax. And then do something with that parameter, call helper method or whatever and slot at the beginning/end, however you like.
-
Now that is clear as mud.
I have control over the SLOT - that is not the issue.
My question is
if the SIGNAL contains , carries or whatever term is used , the "item" (as a parameter) without specifying "item' in ":connect" as parameter
AND
SLOT connects to such SIGNAL , again without specifying it in "connect"
syntax
HOW DO I ACCESS THE "item" passed (?) by SIGNAL in my SLOT function ?
So far all I have done is to activate the SLOT function - now I need to learn how to use the parameter carried in by SIGNAL. -
Hi,
Did you go through the Signals and Slots chapter in Qt's documentation ?
It explains everything and provides simple examples to show how things work. -
@SGaist Just scanned thru thru this .
https://wiki.qt.io/New_Signal_Slot_Syntax
Did not find a single example how SIGNAL with parameter is used in SLOT.
It does not explain how such parameter gets thru to the SLOT also.
There is no SLOT function example either.
I'll keep looking , there got to be a pony somewhere... -
The Counter class from the documentation:
#include <QObject> class Counter : public QObject { Q_OBJECT public: Counter() { m_value = 0; } int value() const { return m_value; } public slots: void setValue(int value); signals: void valueChanged(int newValue); private: int m_value; };
The setValue slot:
void Counter::setValue(int value) { if (value != m_value) { m_value = value; emit valueChanged(value); } }
The connection:
Counter a, b; QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
The usage:
a.setValue(12); // a.value() == 12, b.value() == 12 b.setValue(48); // a.value() == 12, b.value() == 4
When you call setValue from object a and that this value does not match the current counter, the valueChanged signal will be emitted with the new value as parameter.
Then, the setValue of b will be called with as parameter the new value. -
@AnneRanch said in How to "emit " SIGNAL " after "addItem"?:
It does not explain how such parameter gets thru to the SLOT also.
When you are using the new style signal-slot connections then the C++ compiler checks that the sender and receiver have matching signatures (or close enough) at compile time and fails to compile if they do not.
https://wiki.qt.io/New_Signal_Slot_Syntax#Type_mismatchThere's not much to explain here to the user of the signal-slot mechanism. The value passed with the signal function call, e.g. fooSignal("barbaz"), is delivered to any slot function connected to the signal essentially as if you had called fooSlot("barbaz"). (The slot may not be called instantly depending on circumstances.) The precise internals of this mechanism are much more involved, but only of relevance if you intend to develop the Qt library yourself.
-
@AnneRanch said in How to "emit " SIGNAL " after "addItem"?:
Now that is clear as mud.
I have control over the SLOT - that is not the issue.My question is
if the SIGNAL contains , carries or whatever term is used , the "item" (as a parameter) without specifying "item' in ":connect" as parameter
AND
SLOT connects to such SIGNAL , again without specifying it in "connect"
syntaxThis don't made sense to me.
Qt has 2 syntax to create a connection:
- the "old" one with SLOT() and SIGNAL() macros
- the "new" one where you can specify the signal and slots functions.
With the old syntax, you have to specify all parameter types (not names), because corresponding slot or signal are found per introspection (all QMeta stuff). So there is no way, at compilation time, to know if signal or slot exists.
With the new syntax, you give full function name. This way it is possible, at compilation, to the check if corresponding signal/slots exists.
With the new syntax you can also connection a signal to a functor/lambda function. Which is not possible with the old syntax.HOW DO I ACCESS THE "item" passed (?) by SIGNAL in my SLOT function ?
So far all I have done is to activate the SLOT function - now I need to learn how to use the parameter carried in by SIGNAL.For all of them, signal and slot do not have to have the same amount of parameters, but signal must at least have same type of parameters as connected slot.
Suppose you have follow signals:
- void statusChanged(int newStatus)
- void newMessage(const QString &message)
- void done()
And following slots:
- void onStatusChanged(int newStatus)
- void onStatusMessageChanged(int newStatus, const QString &message)
- void writeToLog(const QString &message)
- void triggerTimer()
with old syntax connection would be:
connect(this, SIGNAL(statusChanged(int)), this, SLOT(onStatusChanged(int))); connect(this, SIGNAL(statusChanged(int)), this, SLOT(triggerTimer()));
same with new syntax:
connect(this, &MyClass::statusChanged, this, &MyClass::onStatusChanged); connect(this, &MyClass::statusChanged, this, &MyClass::triggerTimer);
But connecting statusChanged with writeToLog will not be possible or onStatusMessageChanged, because parameter types are not compatible.
It seems you are always asking same kind of questions about signals/slots.
There are so many documentation about this- Signals & Slots at doc.qt.io
- What do I do if a slot is not invoked? at KDAB
- How Qt Signals and Slots Work at woboq
I know, reading documentation is boring, but always having same issue is even more boring.
Don't you think so? -
@AnneRanch said in How to "emit " SIGNAL " after "addItem"?:
My question is
if the SIGNAL contains , carries or whatever term is used , the "item" (as a parameter) without specifying "item' in ":connect" as parameter
AND
SLOT connects to such SIGNAL , again without specifying it in "connect"
syntax
HOW DO I ACCESS THE "item" passed (?) by SIGNAL in my SLOT function ?When you do the
connect()
you specify the function signature. The signature consists of the types of the parameters, not the object you like to pass. The slot you want to connect to needs to have the same parameter types in the same order. In addition the slot may have fewer parameters by taking away parameters from the end.To take your example from above using
mySignal
,testSlot
andtestSlot_PASS_DATA
I will try to show how to use them.
You should have 1 signal in this example:signals: void mySignal(QListWidgetItem*); // you are allowed to give a parameter name, but it is not necessary
You can have two different kinds of slots to be connected to this signal:
public slots: void testSlot(); void testSlot_PASS_DATA(QListWidgetItem *item);
You can do a connect from
mySignal
totestSlot
as the slot has fewer parameters as the signal. You can connectmySignal
totestSlot_PASS_DATA
because the two have the same signature (i.e. the same types of their respective parameters).How do you use these?
emit mySignal(item);
When you emit the signal, you specify which object/value should be handed down. Qt will then take this object and hands it to the slots it calls. This is how you acces the item in testSlot_PASS_DATA. These are the basics of Qt. If you've had read thoroughly through the documentation you would know that already.
-
@SimonSchroeder We have beek round and round and I stiil do not see as real code for the SLOT.
The SIGNAL is pretty much covered, but NOT the SLOT.
I do noty have an acces to my code riight now so this is just a fake// SLOT function declaration
void testSlot_PASS_DATA(QListWidgetItem *item);
// function definiton
void testSlot_PASS_DATA(QListWidgetItem *item)
{
// display item
ui-> add ....(item);}
SIGNAL :
emit value_changed(item) ;
// the SLOAT is connected to SIGNAL
connect (this, SIGNAL( value_chnged);
this , SLOT( testSlot_PASS_DATA);I really appreciate all the help, but could do without constant innuendo about RTFM.
Give it a rest.
I''ll be back when I get my OS in order. -
@SGaist said in How to "emit " SIGNAL " after "addItem"?:
The Counter class from the documentation:
#include <QObject> class Counter : public QObject { Q_OBJECT public: Counter() { m_value = 0; } int value() const { return m_value; } public slots: void setValue(int value); signals: void valueChanged(int newValue); private: int m_value; };
The setValue slot:
void Counter::setValue(int value) { if (value != m_value) { m_value = value; emit valueChanged(value); } }
The connection:
Counter a, b; QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
The usage:
a.setValue(12); // a.value() == 12, b.value() == 12 b.setValue(48); // a.value() == 12, b.value() == 4
When you call setValue from object a and that this value does not match the current counter, the valueChanged signal will be emitted with the new value as parameter.
Then, the setValue of b will be called with as parameter the new value.\
I am really mixed up with this sample.I was under the assumption that "emit" generates the SIGNAL , at least in my case.
I do not have "value " , one of the results of my code is "item" .
It is a local variable witch gets assigned (new) , hence not really changed.Can you confirm just this part for me ?