Solved Signal emitted but Slot won't work
-
@Eeli-K
Thank you for a reply. So apparently the object 'co' in otherfunction.cpp and 'cco' in mainwindow.cpp are different.Then should I call 'cco' in mainwindow.cpp?
Sorry I know I am asking basic things but I wanted to understand Signals and Slot better.
-
Two different instances of the same type of object.
coo is a pointer so you have to use it where you can "see" it. You connected that objects signal to the slot so yes you must use the same pointer to the object to make it emit the signal. as @Eeli-K and @dheerendra have said. -
@kenchan
Thanks a lot.
It is true that I created cco in mainwindow.cpp and connected it in mainwindow.cppbut is there a way to 'see' cco in otherfunction.cpp ?
-
where is other function relative to main window and cco?
you could:
pass the pointer to the other function directly
or
make cco a member of mainwindow and make mainwidow visible to other function and have a getter function in mainwindow
... etc. -
@checkers This is quite basic thing in C++, you clearly need practice in it and unfortunately there's no other way to learn it than practice, fail first and then succeed :) Eventually you will understand how to govern the lifetime of objects, where and when to create and destroy them and how objects can have access to or reach other objects. In C++ you can for example pass pointers and references as arguments to functions and store objects as member data. Kenchan already gave tips for this situation. It's difficult to say more without seeing the whole code.
-
@kenchan
I call some functions in otherfunction.cpp from mainwindow.cpp by including otherfunction.h
I wanted to call cco from those functions in otherfunction.cpp likeotherfunction.cpp
void testfunction()
{
cco->setString("test");
}
mainwindow.cpp
void MainWindow::on_pushButton_clicked()
{
testfunction();
}I will try passing cco directly to my function in otherfunction.cpp
thank you very much
-
@Eeli-K
Thank you. I know I need some practice in here as I am a novice to C++ also.
I will try to solve current situation. -
@checkers Why does otherfunction.cpp need access to cco if cco is part of main window? Doing it like this is bad design. This testfunction could just return the string and you call cco then in mainwindow:
QString testfunction() { return "test"; } void MainWindow::on_pushButton_clicked() { cco->setString(testfunction()); }
-
@jsulm
Thank you for advice
You sound correctI may be confused the concept of Signals and Slot that Signals must be called from other place than Slot
-
@checkers Well, slots are usually defined in other classes than signals. That is the whole idea behind signals/slots: that an object can notify other objects via signals. The object which emits a signal usually does not know who is connected to this signal nor does it care - it just emits the signal, who ever wants receives it.