Solved Signal and slot from other class not working
-
Set up the connect in your constructor and emit the signal, you connect to, in your function A to execute function B in your second class.
-
This only works when I setup in main.cpp and when it fires in won't update anything in the function except print. When I set up in the constructor and place the emit in the function it does not fire,
QObject::connect(&timer, SIGNAL(printPerSecond()), &w, SLOT(anotherFuncion())); emit printPerSecond()
-
@Ucn_ What is timer in your connect?
Is there a warning at runtime that connect failed?
You should use the new connect syntax to get compiler error instead of runtime warning if your connect call is wrong. -
@jsulm It does not give any warning. I tried different connects, it just won't execute the function.
-
This post is deleted! -
@Ucn_ Without more code I can't tell you what the problem is. One possibility: "timer" is not the same instance from where you emit the signal.
-
@jsulm I have as follow in my MainWindow constructor:
Someclass send; QObject::connect(&send, SIGNAL(valueChanged()), SLOT(someFunction()));
I have this signal in Someclass
signals: void valueChanged();
And this Slot in my MainWindow
private slots: void someFunction();
then I emit from a function
void Someclass::anotherfunction(){ emit valueChanged(); }
-
@Ucn_ said in Signal and slot from other class not working:
Someclass send;
QObject::connect(&send, SIGNAL(valueChanged()), SLOT(someFunction()));Here send is a local variable and is destroyed as soon as it goes out of scope!
-
@jsulm If I declare Someclass *send = new Someclass(); it gives error:
mainwindow.cpp:18:14: error: no matching member function for call to 'connect' qobject.h:467:41: note: candidate function not viable: no known conversion from 'Someclass **' to 'const QObject *' for 1st argument; remove & qobject.h:264:13: note: candidate template ignored: requirement 'int(QtPrivate::FunctionPointer<const char *>::ArgumentCount) >= 0' was not satisfied [with Func1 = const char *, Func2 = const char *] qobject.h:304:13: note: candidate template ignored: substitution failure [with Func1 = const char *, Func2 = const char *]: no type named 'Object' in 'QtPrivate::FunctionPointer<const char *>' qobject.h:232:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided qobject.h:273:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided qobject.h:312:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided qobject.h:212:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided qobject.h:215:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
I handle keypress event in Someclass, but is just an example. I want to emit valueChanged(); when a specific key is pressed or when a function is run, even though I did remove & to:
Someclass *send = new Someclass() QObject::connect(send, SIGNAL(valueChanged()), SLOT(someFunction()));
Still doesn't fire.
-
@Ucn_ Don't you have send as class member?
Regarding error: simply remove & in front of send in connect as send is now already a pointer.QObject::connect(send, SIGNAL(valueChanged()), SLOT(someFunction()));