signal/slot between two different classes
-
@dziko147 Did you read what I and @KroMignon wrote?
-
@dziko147 said in signal/slot between two different classes:
so logically it should works , there is no syntax error ! ?
Are you jocking?
Nothing is correct:
- the slot name is wrong
- the
SIGNAL()
syntax is wrong ==> only signal signature means: signal name and parameter types - the
SLOT()
syntax is wrong ==> only slot signature means: slot name and parameter types
-
@dziko147 said in signal/slot between two different classes:
ok could you provide a connect implementation to solve this .
@jsulm already told you what's wrong and you should switch over to the new signal/slot syntax so you get proper compile time errors instead during runtime.
-
@dziko147 said in signal/slot between two different classes:
ok could you provide a connect implementation to solve this .
i am blocked since 3 days :/Can you read?
@jsulm told you what to do:
Remove "value". Old style connect does only contain the parameter type, not its name.
And in Qt documentation:
Note that the signal and slots parameters must not contain any variable names, only the type. E.g. the following would not work and return false:
// WRONG QObject::connect(scrollBar, SIGNAL(valueChanged(int value)), label, SLOT(setNum(int value))); // CORRECT QObject::connect(scrollBar, SIGNAL(valueChanged(int)), label, SLOT(setNum(int)));
I cannot believe this take 3 days to understand.
-
@KroMignon even i remove the "value" it doesn't works .
I didn't understand why .
No error but the application spits . -
@dziko147 said in signal/slot between two different classes:
No error but the application spits .
Why not using the debugger to find out why your application crash?
And in you connect:
connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
What is
back
?
Where is it defined? -
@dziko147 Next thing to check: what does connect() call return?
qDebug() << connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
Next: did you make sure that the signal is actually emitted?
if (m_canDevice) { qDebug() << "Emit signal"; emit canDeviceChanged(1); }
-
Hi,
@dziko147 said in signal/slot between two different classes:
qDebug() << connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
return true .
And the signal is emmited
Where is back defined ?
Any chances that this is a function local variable that is destroyed at the end of it ? -
The question is: where is it defined ?
-
That's what I suspected: back is destroyed at the end of the constructor.
-
@dziko147 said in signal/slot between two different classes:
{
m_ui->setupUi(this); Backend back ; connect(this, SIGNAL(canDeviceChanged(int)),&back, SLOT(statuspican(int)));
I know that you are a C++/Qt beginner, but as many times written: please take time to understand C++ basics.
C++ variable scope and life-cycle is one of them:
- https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm
- https://www.geeksforgeeks.org/scope-of-variables-in-c/
- there are many more available on internet
If you don't learn, how do you expect programming?
-
@KroMignon Hello
to create a global variable . Can i create Backend back; in MainWindow.h . -
@dziko147 said in signal/slot between two different classes:
to create a global variable . Can i create Backend back; in MainWindow.h .
I don't want to be pedantic, but as you seems to be very unexperimented I will try to be as clear as possible.
There is no need of a global variable at all.
For me, there are 2 clean way to do it:
- create a variable in
main()
and do all connection there. - add a class attribute from type
Backend
intoMainWindow
and eventually give access to it through getter.
My preferred way is the second.
But this is up to you. - create a variable in