Signal and slot from other class not working
-
@Cobra91151 Thanks, I will give it a try.
-
@Cobra91151 said in Signal and slot from other class not working:
connect(ui->pushButton, &QPushButton::clicked, testLabel {
testLabel->setText("Some text from button click");
});The example works, thank. But, is there a way I can pass functions or widgets created in designer to the lambda capture list? Let's say I want to run a function after QLabel click or change a specific label.
-
@mrjj Thanks, It works. however, I don't understand why it works in one example and in the other says
'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const': cannot convert argument 1 from 'QLabel *' to 'const ClickableLabels *'
-
Because you are using a pointer to a QLabel as first parameter and a signal from your ClickableLabel as second parameter.
-
Ensure your original parameter is a ClickableLabel instance.
-
Since you are using Designer, yes.
-
I know I marked this as solved, and thanks to everyone who helped me understand what I was missing. I'm able to make it work when is widget to widget or promoted with with custom events. However when there is nothing triggered or clicked like a button I'm not able to connect. For example. Function to function, if one function from another class is executed, connect and execute another function in another class. Where the signal is the first executed function and slot is the function executed after the first was triggered. Thanks
-
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()
-
@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.