signal/slot between two different classes
-
@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