Solved Signals and Slots between two classes
-
I am facing problem on connecting the signals between two classes. I have create slots, signals and connected the same but signals are not received in slots. Could you please help me out to find the problem. Please find the code which I have implemented.
Form.H
#ifndef FORM_H #define FORM_H #include <QWidget> #include "test.h" namespace Ui { class Form; } class Form : public QWidget { Q_OBJECT public: explicit Form(QWidget *parent = nullptr); ~Form(); signals: void valueChanged(int newValue); private slots: void on_button_clicked(); void off_button_clicked(); private: Ui::Form *ui; int value; Test obj; }; #endif // FORM_H
Test.h
#ifndef TEST_H #define TEST_H #include <QtCore/qglobal.h> #if QT_VERSION >= 0x050000 #include <QtWidgets/QWidget> #else #include <QtGui/QWidget> #endif class Test : public QWidget { Q_OBJECT public: explicit Test(QWidget *parent = nullptr); void EnableImage(int value); signals: void ValueChanged(int value); }; #endif // TEST_H
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QLabel> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); ~Widget(); QLabel *Picture; public slots: void setValue(int value); public: Ui::Widget *ui; }; #endif // WIDGET_H
form.cpp
#include "form.h" #include "ui_form.h" Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) { ui->setupUi(this); value = 0; Test obj; } Form::~Form() { delete ui; } void Form::on_button_clicked() { value = 1; obj.EnableImage(value); } void Form::off_button_clicked() { value = 0; obj.EnableImage(value); }
main.cpp
#include "widget.h" #include "form.h" #include "test.h" #include <QApplication> #include <QObject> extern Widget w; int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; Form f; Test t; w.show(); f.show(); // help show windows apart f.move(w.geometry().left() + 50,w.geometry().top() + 50); QObject::connect(&t, SIGNAL(valueChanged(int)), &w, SLOT(setValue(int))); return a.exec(); }
test.cpp
#include "test.h" Test::Test(QWidget *parent) : QWidget(parent) { } void Test::EnableImage(int value) { emit ValueChanged(value); } widget.cpp #include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); Picture = ui->image; Picture->hide(); } void Widget::setValue(int value) { if(value == 0) { Picture->hide(); } else { Picture->show(); } } Widget::~Widget() { delete ui; }
-
Can you use the new connection syntax?
QObject::connect(&t, SIGNAL(valueChanged(int)),&w, SLOT(setValue(int)));
should becomeQObject::connect(&t,&Test::valueChanged,&w,&Widget::setValue);
-
@VRonin no change. signals are not recieved in slots.
-
@ktrsathish said in Singals and Slots between two classes:
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
value = 0;
Test obj;
}What should 'Test obj' do here?
void Form::on_button_clicked()
{
value = 1;
obj.EnableImage(value);
}This is not the object you're doing your connect on.
-
@Christian-Ehrlicher My actual implementaion shall follow the above architecture. so I have written the sample code to work first.
-
@ktrsathish
Hi
Could you zip sample and upload via
https://www.justbeamit.com/( single use :( )
any other dropbox, Google drive, etc that can give a link
with no registration required to get it.Code looks ok, but its easy to miss stuff due to all the scrolling.
-
@mrjj https://drive.google.com/file/d/1d0Cc86uItT6F-ZzpbWtreATYICApRG1U/view?ts=5bd73a93
Please check the code and let me know the details. -
@ktrsathish
super !
but it asks permissions.
Can you remove that ? -
-
@ktrsathish
Hi
In class Form, you have
private:
..
Test obj;and you use that to emit
void Form::on_ABS_ON_clicked()
{
value = 1;
obj.EnableImage(value);
}BUT
the one you hook up in main is another test instance
and its not used to emit the signal.Test t; QObject::connect(&t, &Test::ValueChanged, &w, &Widget::setValue); t is not the one u use to emit signal.
So that is why all looks fine but nothing happens.
The actual emitter object is not connected up.So it was as mr. @Christian-Ehrlicher said / suggested
-
@mrjj said in Signals and Slots between two classes:
Test t;
QObject::connect(&t, &Test::ValueChanged, &w, &Widget::setValue);Thanks. It is solved.