Help with signal and slots (connect in slot of another file)

4

Good morning dear,

I created a Qt Widget Application with Qt Creator (Qt 5.6.1).

The project has the following structure:

  • myproject.pro

    • Headers

      • dialogform.h

      • mainwindow.h

    • Sources

      • dialogform.cpp

      • main.cpp

      • mainwindow.cpp

    • Forms

      • dialogform.h

      • mainwindow.h

When I click on the DialogForm pushbutton, I need to call the clear () function of MainWindow

I've heard that I need to do this through signals and slots. I've read the documentation about it, but I still can not make the connection.

Could someone help me with this connect?

Thank you very much

dialogform.h

#ifndef DIALOGFORM_H
#define DIALOGFORM_H

#include <QDialog>

namespace Ui {
class DialogForm;
}

class DialogForm : public QDialog
{
    Q_OBJECT

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

private slots:


private:
    Ui::DialogForm *ui;
};

#endif // DIALOGFORM_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void clear();

private slots:
    void on_pbCallDialog_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

dialogform.cpp

#include "dialogform.h"
#include "ui_dialogform.h"
#include "mainwindow.h"

DialogForm::DialogForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogForm)
{
    ui->setupUi(this);
    connect(); // ajuda aqui.
}

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

main.cpp

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

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

    return a.exec();
}

mainwindow.cpp

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

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

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

void MainWindow::on_pbCallDialog_clicked()
{
    DialogForm *dialogForm = new DialogForm(this);
    dialogForm->show();
}

void MainWindow::clear()
{
    ui->lineEdit->clear();
}
    
asked by anonymous 24.01.2017 / 13:18

1 answer

4

Here's a suggested solution.

The class DialogForm already receives the pointer of the instance of class MainWindow in constructor (via variable parent ). Then, in this constructor, make the connection as follows:

DialogForm::DialogForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogForm)
{
    ui->setupUi(this);
    connect(ui->pushButton, &QPushButton::clicked, static_cast<MainWindow*>(parent), &MainWindow::clear); // <=== Aqui faz a conexão
}

Notice that the clear method of class MainWindow has already been declared as a slot in the header file. However, it does not have the same clicked sign of class QPushButton (which includes a Boolean parameter). As the parameter has a default, it will work without problems (I just tested!).

But it's always important to be careful to note if the signatures are the same. In other cases, you may need to declare the clear slot in the same way as the signal. Example of what it would be like, there in the definition of class MainWindow :

public slots:
    void clear(bool checked = false);

Or you may need to use another signal with the same signature (in this example, perhaps pressed , which is issued even before the mouse button is released - unlike the clicked that waits for the button to be released).

    
24.01.2017 / 13:30