0
votes

I'm trying to connect two frames with a custom signal but I'm not really getting it.

This code is just an example of what im trying to do in my program, my objective is to transfer data between frames.

Files:

(sender)

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    signals:
        void send();

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

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

    #endif // MAINWINDOW_H

On "mainwindow.cpp" I've got the void on_pushButton_clicked() that emits the signal and shows the new frame:

private slot void:

        void MainWindow::on_pushButton_clicked()
    {
        emit send();
        Dialog sw;
        sw.setModal(true);
        sw.exec();
    }

(receiver):

        #ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>
    #include <QDebug>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
        Q_OBJECT

    public:
        explicit Dialog(QWidget *parent = nullptr);
        ~Dialog();

    private slots:
        void receive();

    private:
        Ui::Dialog *ui;
        int a;
    };

    #endif // DIALOG_H

and the .cpp:

        #include "dialog.h"
    #include "ui_dialog.h"
    #include "mainwindow.h"


    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        a=0;
        MainWindow w;
        connect(&w, SIGNAL(send()), this, SLOT(receive()));
        qDebug() << a;
    }


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

    void Dialog::receive(){
        qDebug() << "ola";
        a++;
    }

Conclusion: So basicly the function Dialog doesn't print the qDebug(), and 'a' is still 0, so I conclude that the connection isn't set/executed.

Thanks all, Best regards, Dylan Lopes.

edit: Wrote a conclusion on the end of the post.

2
We are not getting it either. What's the problem?Matthieu Brucher
Sorry, forgot to mencion it, i edited a conclusion on the end of the post.Dylan Lopes
'On "mainwindow.cpp" i've got the void on_pushButton_clicked() that emits the signal and shows the new frame,'Dylan Lopes
Your object "MainWindow w" was created in the constructor of the Dialog so when it go out of the scope of the view it will be deleted, so try to move it to private section of the class and I think all'll be okAlexander Chernin

2 Answers

0
votes

Consider the code in your Dialog constructor...

MainWindow w;
connect(&w, SIGNAL(send()), this, SLOT(receive()));

This creates a locally scoped MainWindow on the stack and connects its send() signal to the Dialog's receive() slot. But the MainWindow -- and, hence, the connection -- will be destroyed as soon as the Dialog constructor has completed.

In addition, looking at MainWindow::on_pushButton_clicked...

void MainWindow::on_pushButton_clicked()
{
    emit send();
    Dialog sw;
    sw.setModal(true);
    sw.exec();
}

You emit the send() signal before constructing the Dialog.

I don't really know enough about what you're trying to achieve to provide a definitive answer, but in the interests of getting some kind of signal/slot interaction you might want to do the following: change the Dialog constructor to...

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        a=0;
        qDebug() << a;
    }

And change MainWindow::on_pushButton_clicked to...

void MainWindow::on_pushButton_clicked()
{
    Dialog sw;
    connect(this, &MainWindow::send, &sw, &Dialog::receive);
    emit send();
    sw.setModal(true);
    sw.exec();
}

That should at least result in Dialog::receive being invoked and you can work from there.

0
votes

Connection between a signal and a slot doesn't mean that the signal function will be triggered.

You still need to emit your signal so that a gets updated.

Creating an empty slot isn't working either, as slots are the receiving point of a signal. In this case, on_pushButton_clicked() gets triggered whent he push button is clicked. This doesn't trigger send unless you call EMIT(send()) (IIRC, you emit a signal with EMIT, is that still the case?).