cenixo deolin
cenixo deolin

Reputation: 71

Example SLOT/SIGNAL between two object QT

My app, consists in 2 different object (QObject and QMainWIndow), and I am wondering how to communicate between them with SLOT/SIGNAL. Moreover, does existing better approach ?

Can someone make an simple example ? Thank :)

sample

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_mainwindow.h"
#include "object.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

private slots:


};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "object.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->chkState, SIGNAL(clicked()), this, SLOT(object->chkState();));
}

MainWindow::~MainWindow()
{
    delete ui;
}

object.h

#ifndef OBJET_H
#define OBJET_H

#include "mainwindow.h"
#include <QMainWindow>
#include <QObject>

class Object : public QObject
{
    Q_OBJECT
public:
    explicit Object(QObject *parent = 0);
    bool state;

signals:

private slots:
    void chkState(Ui::MainWindow *ui);
};

#endif // OBJET_H

objet.cpp

#include "object.h"
#include "mainwindow.h"

Object::Object(QObject *parent) : QObject(parent)
{

}

void Object::chkState(Ui::MainWindow *ui)
{
    if (ui->chkState->isChecked())
    {
        ui->state->setText("true");
        state = true;
    }
    else
    {
        ui->state->setText("false");
        state = false;
    }
}

Upvotes: 5

Views: 16532

Answers (2)

Gabrielle de Grimouard
Gabrielle de Grimouard

Reputation: 2005

There are several errors in your code. First:

connect(ui->chkState, SIGNAL(clicked()), this, SLOT(object->chkState();));

Here you say: "when we click on the ui->chkState, I want you call a function in this, which is the object->chkState slot". That's definitly not what you want. What is object ? this object hasn't been created. What you want is :

connect(ui->chkState, SIGNAL(clicked()), myobject, SLOT(chkState()));

with myobject an object of type Object so you need to add in your mainwindow.h a

Object *myobject;

and in your mainwindow.cpp before the connect:

myobject = new Object(this);

Moreover, your function void chkState(Ui::MainWindow *ui); won't work because you cannot get the mainwindow ui in parameter like that. What I advise you to do, if it's only for tests so you know that parent is the type of MainWindow, you can do:

void Object::chkState()
{
    MainWindow* parent = static_cast<MainWindow*>(parent());
    if (parent->ui->chkState->isChecked())
    {
        parent->ui->state->setText("true");
        state = true;
    }
    else
    {
        parent->ui->state->setText("false");
        state = false;
    }
}

So the parameter in your slot is removed.

Upvotes: 1

agent_bean
agent_bean

Reputation: 1585

Here is a simple example of how to emit signals and slots.

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QMainWindow>
        #include "object.h"

        namespace Ui {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
            Q_OBJECT

        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();

        signals:
            void transmit_to_object(bool value);

        private slots:
            void receive_from_object(bool value);

            void on_checkBox_clicked();

        private:
            Ui::MainWindow *ui;
            object          m_object;
        };

        #endif // MAINWINDOW_H

    #ifndef OBJECT_H
    #define OBJECT_H

    #include <QObject>

    class object : public QObject
    {
        Q_OBJECT

        public:
            explicit object(QObject * parent = 0);

        signals:
            void transmit_to_gui(bool value);

        private slots:
            void receive_from_gui(bool value);

        private:
        bool state;

    };

    #endif // OBJECT_H

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(&m_object,SIGNAL(transmit_to_gui(bool)),this,SLOT(receive_from_object(bool)));

    connect(this,SIGNAL(transmit_to_object(bool)),&m_object,SLOT(receive_from_gui(bool)));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::receive_from_object(bool value)
{
    if(value)
    {
        ui->lineEdit->setText("true");
    }
    else
    {
        ui->lineEdit->setText("false");
    }
}

void MainWindow::on_checkBox_clicked()
{
    if(ui->checkBox->isChecked())
    {
        emit transmit_to_object(true);
    }
    else
    {
        emit transmit_to_object(false);
    }

}

#include "object.h"
#include "mainwindow.h"

object::object(QObject *parent)
{

}

void object::receive_from_gui(bool value)
{
    state = value;

    emit transmit_to_gui(state);
}

Upvotes: 8

Related Questions