Signal and slot from other class not working
-
wrote on 23 Nov 2019, 20:59 last edited by
The signal and slot connection I commented on the main.cpp fires, but it does not update the Qlabel and it does not emit on button click.
-
The signal and slot connection I commented on the main.cpp fires, but it does not update the Qlabel and it does not emit on button click.
@Ucn_ But your current code can not work as I already explained - you are connecting to another instance of ClickableLabels than the one you're displaying.
-
@Ucn_ But your current code can not work as I already explained - you are connecting to another instance of ClickableLabels than the one you're displaying.
wrote on 23 Nov 2019, 21:36 last edited by@Christian-Ehrlicher I would like to emit from the ClickableLabels class. How do I achieve that? I'm kind of lost.
-
@Christian-Ehrlicher I would like to emit from the ClickableLabels class. How do I achieve that? I'm kind of lost.
wrote on 23 Nov 2019, 22:30 last edited by Cobra91151Hello!
I have created a simple example to illustrate a clickable label, button and connect using lambdas, so you can simplify your code.
clickablelabel.h
#ifndef CLICKABLELABEL_H #define CLICKABLELABEL_H #include <QWidget> #include <QLabel> #include <QEvent> class ClickableLabel : public QLabel { Q_OBJECT public: using QLabel::QLabel; signals: void clicked(); protected: void mousePressEvent(QMouseEvent *event) override; }; #endif // CLICKABLELABEL_H
clickablelabel.cpp
#include "clickablelabel.h" void ClickableLabel::mousePressEvent(QMouseEvent *event) { emit clicked(); QLabel::mousePressEvent(event); }
test.h
#ifndef TEST_H #define TEST_H #include <QDialog> #include <QVBoxLayout> #include "clickablelabel.h" namespace Ui { class Test; } class Test : public QDialog { Q_OBJECT public: explicit Test(QWidget *parent = nullptr); ~Test(); private: Ui::Test *ui; }; #endif // TEST_H
test.cpp
#include "test.h" #include "ui_test.h" Test::Test(QWidget *parent) : QDialog(parent), ui(new Ui::Test) { ui->setupUi(this); setFixedSize(600, 500); setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); ClickableLabel *testLabel = new ClickableLabel("This is a test label...", this); connect(testLabel, &ClickableLabel::clicked, [testLabel]() { testLabel->setText("Some text from mouse click"); }); ui->pushButton->setText("Test button"); connect(ui->pushButton, &QPushButton::clicked, [testLabel]() { testLabel->setText("Some text from button click"); }); QVBoxLayout *testLayout = new QVBoxLayout(); testLayout->setAlignment(Qt::AlignCenter); testLayout->addWidget(testLabel); testLayout->addWidget(ui->pushButton); setLayout(testLayout); } Test::~Test() { delete ui; }
Screenshot:
Happy coding!
-
Hello!
I have created a simple example to illustrate a clickable label, button and connect using lambdas, so you can simplify your code.
clickablelabel.h
#ifndef CLICKABLELABEL_H #define CLICKABLELABEL_H #include <QWidget> #include <QLabel> #include <QEvent> class ClickableLabel : public QLabel { Q_OBJECT public: using QLabel::QLabel; signals: void clicked(); protected: void mousePressEvent(QMouseEvent *event) override; }; #endif // CLICKABLELABEL_H
clickablelabel.cpp
#include "clickablelabel.h" void ClickableLabel::mousePressEvent(QMouseEvent *event) { emit clicked(); QLabel::mousePressEvent(event); }
test.h
#ifndef TEST_H #define TEST_H #include <QDialog> #include <QVBoxLayout> #include "clickablelabel.h" namespace Ui { class Test; } class Test : public QDialog { Q_OBJECT public: explicit Test(QWidget *parent = nullptr); ~Test(); private: Ui::Test *ui; }; #endif // TEST_H
test.cpp
#include "test.h" #include "ui_test.h" Test::Test(QWidget *parent) : QDialog(parent), ui(new Ui::Test) { ui->setupUi(this); setFixedSize(600, 500); setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); ClickableLabel *testLabel = new ClickableLabel("This is a test label...", this); connect(testLabel, &ClickableLabel::clicked, [testLabel]() { testLabel->setText("Some text from mouse click"); }); ui->pushButton->setText("Test button"); connect(ui->pushButton, &QPushButton::clicked, [testLabel]() { testLabel->setText("Some text from button click"); }); QVBoxLayout *testLayout = new QVBoxLayout(); testLayout->setAlignment(Qt::AlignCenter); testLayout->addWidget(testLabel); testLayout->addWidget(ui->pushButton); setLayout(testLayout); } Test::~Test() { delete ui; }
Screenshot:
Happy coding!
wrote on 23 Nov 2019, 23:11 last edited by Ucn_@Cobra91151 Thanks, I will give it a try.
-
Hello!
I have created a simple example to illustrate a clickable label, button and connect using lambdas, so you can simplify your code.
clickablelabel.h
#ifndef CLICKABLELABEL_H #define CLICKABLELABEL_H #include <QWidget> #include <QLabel> #include <QEvent> class ClickableLabel : public QLabel { Q_OBJECT public: using QLabel::QLabel; signals: void clicked(); protected: void mousePressEvent(QMouseEvent *event) override; }; #endif // CLICKABLELABEL_H
clickablelabel.cpp
#include "clickablelabel.h" void ClickableLabel::mousePressEvent(QMouseEvent *event) { emit clicked(); QLabel::mousePressEvent(event); }
test.h
#ifndef TEST_H #define TEST_H #include <QDialog> #include <QVBoxLayout> #include "clickablelabel.h" namespace Ui { class Test; } class Test : public QDialog { Q_OBJECT public: explicit Test(QWidget *parent = nullptr); ~Test(); private: Ui::Test *ui; }; #endif // TEST_H
test.cpp
#include "test.h" #include "ui_test.h" Test::Test(QWidget *parent) : QDialog(parent), ui(new Ui::Test) { ui->setupUi(this); setFixedSize(600, 500); setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); ClickableLabel *testLabel = new ClickableLabel("This is a test label...", this); connect(testLabel, &ClickableLabel::clicked, [testLabel]() { testLabel->setText("Some text from mouse click"); }); ui->pushButton->setText("Test button"); connect(ui->pushButton, &QPushButton::clicked, [testLabel]() { testLabel->setText("Some text from button click"); }); QVBoxLayout *testLayout = new QVBoxLayout(); testLayout->setAlignment(Qt::AlignCenter); testLayout->addWidget(testLabel); testLayout->addWidget(ui->pushButton); setLayout(testLayout); } Test::~Test() { delete ui; }
Screenshot:
Happy coding!
wrote on 24 Nov 2019, 05:57 last edited by Ucn_@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.
-
@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.
Hi
You can capture "this" to allow to use both
UI->xxx and member functionsconnect(ui->pushButton, &QPushButton::clicked, [this]() { ui->xxxxx; });
-
Hi
You can capture "this" to allow to use both
UI->xxx and member functionsconnect(ui->pushButton, &QPushButton::clicked, [this]() { ui->xxxxx; });
wrote on 24 Nov 2019, 21:32 last edited by@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.
-
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.
-
wrote on 25 Nov 2019, 10:54 last edited by Ucn_
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
-
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
-
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.
wrote on 25 Nov 2019, 13:27 last edited byThis 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()
-
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. -
@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. -
wrote on 25 Nov 2019, 13:47 last edited byThis post is deleted!
14/29