Results 1 to 6 of 6

Thread: How correctly to call a function from another file?

  1. #1
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default How correctly to call a function from another file?

    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
    Qt Code:
    1. #ifndef DIALOGFORM_H
    2. #define DIALOGFORM_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class DialogForm;
    8. }
    9.  
    10. class DialogForm : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit DialogForm(QWidget *parent = 0);
    16. ~DialogForm();
    17.  
    18. private slots:
    19. void on_pbClearLineEdit_clicked();
    20.  
    21. private:
    22. Ui::DialogForm *ui;
    23. };
    24.  
    25. #endif // DIALOGFORM_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17. void clear();
    18.  
    19. private slots:
    20. void on_pbCallDialog_clicked();
    21.  
    22. private:
    23. Ui::MainWindow *ui;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "dialogform.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::on_pbCallDialog_clicked()
    18. {
    19. DialogForm *dialogForm = new DialogForm(this);
    20. dialogForm->show();
    21.  
    22. // OR
    23. // DialogForm d;
    24. // d.exec();
    25.  
    26. }
    27.  
    28. void MainWindow::clear()
    29. {
    30. ui->lineEdit->clear();
    31. }
    To copy to clipboard, switch view to plain text mode 

    dialogform.cpp
    Qt Code:
    1. #include "dialogform.h"
    2. #include "ui_dialogform.h"
    3. #include "mainwindow.h"
    4.  
    5. DialogForm::DialogForm(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::DialogForm)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. DialogForm::~DialogForm()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void DialogForm::on_pbClearLineEdit_clicked()
    18. {
    19. MainWindow m;
    20. m.clear(); // Here, is not clearing the MainWindow's lineEdit
    21. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by juliano.gomes; 23rd January 2017 at 20:21.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How correctly to call a function from another file?

    The problem is that you declare a local object in the function's scope:

    Qt Code:
    1. void DialogForm::on_pbClearLineEdit_clicked()
    2. {
    3. MainWindow m; // <-- this is allocated on the stack and will be deleted at the end of the scope
    4. m.clear(); //
    5. }
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How correctly to call a function from another file?

    Quote Originally Posted by stampede View Post
    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, but it isn't clear in my mind how can i do this connect.

    can you give me some help?

    dialogform.h
    Qt Code:
    1. signals:
    2. void clearMainWindow();
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. public slots:
    2. void clear();
    To copy to clipboard, switch view to plain text mode 

    dialogform.cpp
    Qt Code:
    1. void DialogForm::on_pbClearLineEdit_clicked()
    2. {
    3. connect(sender, SIGNAL(clearMainWindow()),qApp ,SLOT(MainWindow::clear())); // Error: invalid use of non-static member function
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How correctly to call a function from another file?

    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,
    _

  5. #5
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How correctly to call a function from another file?

    Here is the solution:

    Qt Code:
    1. DialogForm::DialogForm(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::DialogForm)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->pbClearLineEdit, &QPushButton::clicked, static_cast<MainWindow*>(parent), &MainWindow::clear);
    7. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How correctly to call a function from another file?

    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,
    _

Similar Threads

  1. How to call a function from another cpp file
    By herculis in forum Newbie
    Replies: 13
    Last Post: 26th September 2014, 09:31
  2. Replies: 2
    Last Post: 2nd December 2013, 04:43
  3. Replies: 3
    Last Post: 17th October 2013, 08:12
  4. Replies: 4
    Last Post: 2nd August 2012, 07:42
  5. How to call cpp file function from qml??
    By naufalahad in forum Qt Quick
    Replies: 5
    Last Post: 30th January 2012, 09:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.