Results 1 to 6 of 6

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.