Solved How to "emit " SIGNAL " after "addItem"?
-
I am doing it wrong....
I need to emit SIGNAL
AFTER item is added /displayedThe "display " is part of SIGNAL / SLOT and its real time varies .
The SLOT verifies some data and only then it does "addItem" so I cannot just add another SLOT to same SIGNAL.//display ui->list->addItem(item);
Do I need to add my own SIGNAL using emit ?
How?PS I have been thru this before - I cannot use any UI 'detect change" of item without physically accessing the item in GUI.
-
@AnneRanch said in How to "emit " SIGNAL " after "addItem"?:
@KroMignon I do not think repeating same over and over will convince me how me creating an instance of item creates a SIGNAL without emit.
Perhaps different explanation would help.There's no "SIGNAL creation".
You create an item -> unrelated to the signal.
You add this item to your list -> unrelated to the signal.
You callvalue_changed(item)
-> still no signal creation.A signal is a function that you declare in your class.
It may or may not have parameter(s) as any other function.emit value_changed(item);
is a function call
value_changed(item);
is exactly the same function callAs was already explained several times:
emit
is a macro that is empty. It's 100% aesthetics. It has even a sibling calledQ_EMIT
which is as empty asemit
.Why is it used ? To make the code clear and easy to reason about.
Is its use mandatory ? No but it leads to code that is hard to reason aboutIt basically tells the reader: when this function is called, all the slots that are connected to it will be called with the parameter(s) you passed to it.
-
@AnneRanch I cannot really understand what problem you try to solve.
Your explanation are very confusing for me.
You can add as many signals (or slots) you need to your class. And when signal is emitted is up to you.Just for clarification:
- a SIGNAL is only a function, which is created at compilation during MOC process (you can find it implementation in <class_name>_moc.cpp).
emit
is just a semantical sugar only to help programmer be aware about calling a SIGNAL function.- Qt is not "real time ", so what do you mean with this?
The "display " is part of SIGNAL / SLOT and its real time varies .
-
I need to convert
//display
ui->list->addItem(item);to SIGNAL to pass the item value to another object.
how?
(Can I get a sample C++ code ?)
Qt is not "real time ", so what do you mean with this?
The SLOT I am processing and displaying the result as "item"
is triggered by real time as a result of search for nearby bluetooth devices. Roughly 2 to 3 seconds. -
@AnneRanch Again, I cannot understand your issue, sorry.
I am not a English native speaker, perhaps my comprehension not so good :(
Creating a signal is trivial:
class MyClass : public QObject { Q_OBJECT public: explicit MyClass(QObject * parent = nullptr): QObject(parent) {} signals: void mySignal(); }
But this cannot be your question, I expect !?!
-
-
Emit your own signal, which passes your
item
to your slot. Do whatever you want and then you can still doui->list->addItem(item);
in your slot to add your item to yourlist
.Similar as @KroMignon wrote above:
signals: void mySignal(QListWidgetItem * item);
-
@KroMignon said in How to "emit " SIGNAL " after "addItem"?:
@AnneRanch I cannot really understand what problem you try to solve.
Your explanation are very confusing for me.
You can add as many signals (or slots) you need to your class. And when signal is emitted is up to you.Just for clarification:
- a SIGNAL is only a function, which is created at compilation during MOC process (you can find it implementation in <class_name>_moc.cpp).
emit
is just a semantical sugar only to help programmer be aware about calling a SIGNAL funct> -emit
is just a semantical sugar only to help programmer be aware about calling a SIGNAL function.ion.- Qt is not "real time ", so what do you mean with this?
The "display " is part of SIGNAL / SLOT and its real time varies .
emit
is just a semantical sugar only to help programmer be aware about calling a SIGNAL function.
Not according to this
-
@AnneRanch
As we have discussed before you see that the definition ofemit
in Qt header file is:#define emit
See https://code.woboq.org/qt5/qtbase/src/corelib/kernel/qobjectdefs.h.html#_M/emit. That's it. So it is "semantical sugar".
So your clazy message shows it looks for whether you place
emit
before calling a signal function, and just warns you if you do not. The code still works unaffected without, it's just trying to make your life better, possibly. -
"real time" has a very specific meaning in embedded and controls programming.
It means a system can satisfy deterministic constraints. This means if the constraint is 100mS then the system will always do certain functions every 100mS. Nit-picky I know, but it can cause confusion in technical discussions.By your usage I think you mean "the time it takes" for the operation to complete.
-
@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.