PDA

View Full Version : How correctly to call a function from another file?



juliano.gomes
23rd January 2017, 19:18
Hello,

I've created an project (Qt Widget Application) with Qt Creator. Qt 5.6.1

My project structure:

myproject.pro
Headers

dialogform.h
mainwindow.h

Sources

dialogform.cpp
main.cpp
mainwindow.cpp

Forms

dialogform.ui
mainwindow.ui



When I click on DialogForm pushbutton, I need to call the clear() function from MainWindow
In my code below, my project is running, but, the clear() function does not clear the lineedit.
Do anyone known how can i fix this?

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:
void on_pbClearLineEdit_clicked();

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();
void clear();

private slots:
void on_pbCallDialog_clicked();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


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();

// OR
// DialogForm d;
// d.exec();

}

void MainWindow::clear()
{
ui->lineEdit->clear();
}


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);
}

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

void DialogForm::on_pbClearLineEdit_clicked()
{
MainWindow m;
m.clear(); // Here, is not clearing the MainWindow's lineEdit
}


main.cpp


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

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

return a.exec();
}

stampede
23rd January 2017, 21:19
The problem is that you declare a local object in the function's scope:



void DialogForm::on_pbClearLineEdit_clicked()
{
MainWindow m; // <-- this is allocated on the stack and will be deleted at the end of the scope
m.clear(); //
}


You should use a signal from the dialog class to inform the MainWindow that the "clear" button was pressed. Declare a signal in your Dialog class and connect it to `MainWindow::clear` slot.

juliano.gomes
23rd January 2017, 23:47
You should use a signal from the dialog class to inform the MainWindow that the "clear" button was pressed. Declare a signal in your Dialog class and connect it to `MainWindow::clear` slot.

Thanks for your help.

I'm trying to connect, so I made the changes below, but i have this error message: invalid use of non-static member function

I've already read about signals and slots (http://doc.qt.io/qt-5/signalsandslots.html), but it isn't clear in my mind how can i do this connect.

can you give me some help?

dialogform.h


signals:
void clearMainWindow();


mainwindow.h


public slots:
void clear();


dialogform.cpp


void DialogForm::on_pbClearLineEdit_clicked()
{
connect(sender, SIGNAL(clearMainWindow()),qApp ,SLOT(MainWindow::clear())); // Error: invalid use of non-static member function
}

anda_skoa
24th January 2017, 09:23
qApp is a pointer to the QApplication object, not your MainWindow object.

The SIGNAL and SLOT macros only take function name and argument types, not class- or namespace names.

Cheers,
_

juliano.gomes
24th January 2017, 13:00
Here is the solution:



DialogForm::DialogForm(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogForm)
{
ui->setupUi(this);
connect(ui->pbClearLineEdit, &QPushButton::clicked, static_cast<MainWindow*>(parent), &MainWindow::clear);
}

anda_skoa
24th January 2017, 13:52
For a clean solution I would recommend to either pass the main window with its full type or to do the connect in the main window.

Cheers,
_