Help with Signal and slots (connect in another file slot)

Asked

Viewed 183 times

4

Good morning dear,

created a project (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 pushbutton Dialogform, need to call the function clear() of Mainwindow

I have heard that I will need to do this through Signals and slots. I have read the documentation about, but I have not yet been able to make the connection.

Could someone give me a hand on 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();
}

1 answer

4


Here is a suggested solution.

The class DialogForm already receives the class instance pointer MainWindow in the constructor (variable track 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
}

Note that the method clear class MainWindow has already been declared as a slot in the header file. However, it does not have the same signature as the sign clicked class QPushButton (which includes a boolean parameter). Since the parameter has a default, it will work smoothly (I just tested!).

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

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

Or maybe you need to use another sign with the same signature (in this example, maybe the pressed, that is issued before the mouse button is released - unlike clicked waiting for the button to be released).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.