Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why Do You Import The mainwindow.h In main.cpp?



  • I followed this example:
    https://wiki.qt.io/How_to_Use_QPushButton
    I did not understand why in main.cpp it imports mainwindow.h. By my understanding of compilation, mainwindow.cpp and main.cpp should either be run together or run sequentially, therefore, I do not understand why mainwindow.h is imported, not mainwindow.cpp. Can you explain to me why this is, and how the compilation would work?


  • Qt Champions 2017

    Welcome to forum. Now some basics of Class declaration & definition is required for you.

    mainwindows.h file has class declaration and mainwindow.cpp has implementation. main.cpp, .h and .cpp files are included for compilation. If you are planning to create the object of mainwindow class in main.cpp then you need include the .h file in main.cpp. It is just find the know the class while creating.



  • @dheerendra But then the implementation in mainwindow.cpp is not executed as main.cpp is called, while there are no imports to mainwindow.cpp, so it is not executed. So how are the definitions of handlebutton() and others defined when mainwindow.cpp is not run? Also, if it is run, when is it run and what controlled it?


  • Qt Champions 2017

    There is nothing like mainwindow.cpp is run. It is part your application. Are you creating the object of mainwindow in main.cpp ? If you are not creating object then you don't need to include in main.cpp.



  • @dheerendra I'm not creating an object in mainwindow.cpp, but I am changing a method of MainWindow in mainwindow.cpp, and if it isn't included, how is the method changed?


  • Qt Champions 2017

    show me the code. Then we will be able to comment on whats happening.



  • It's in the end of the link in the first post, I copied the entire code.


  • Qt Champions 2017

    I don't see the code. Can you paste again ?



  • mainwindow.h

     #ifndef MAINWINDOW_H
     #define MAINWINDOW_H
     
     #include <QMainWindow>
     #include <QPushButton>
     
     namespace Ui {
        class MainWindow;
     }
     
     class MainWindow : public QMainWindow
     {
        Q_OBJECT
     public:
        explicit MainWindow(QWidget *parent = 0);
     private slots:
        void handleButton();
     private:
        QPushButton *m_button;
     };
     
     #endif // MAINWINDOW_H
    

    mainwindow.cpp

     #include "mainwindow.h"
     
     #include <QCoreApplication>
     
     MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
     {
        // Create the button, make "this" the parent
        m_button = new QPushButton("My Button", this);
        // set size and location of the button
        m_button->setGeometry(QRect(QPoint(100, 100),
        QSize(200, 50)));
     
        // Connect button signal to appropriate slot
        connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));
     }
     
     void MainWindow::handleButton()
     {
        // change the text
        m_button->setText("Example");
        // resize button
        m_button->resize(100,100);
     }
    
    

    main.cpp

     #include "mainwindow.h"
     #include <QApplication>
     
     int main(int argc, char *argv[]) 
     {
         QApplication app(argc, argv);
         MainWindow mainWindow;
         mainWindow.showMaximized();
         return app.exec();
     }
    
    

    mainwindow.pro

     QT       += core gui  
     greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
     TARGET = PushButtonExample TEMPLATE = app 
     SOURCES += main.cpp \ mainwindow.cpp  
     HEADERS += mainwindow.h
    

  • Qt Champions 2017

    Look at the following two lines in main.cpp. You are creating the object of class called MainWindow. Declaration of this class in in .h file and implementation is in .cpp file.

    MainWindow mainWindow;
    mainWindow.showMaximized();
    

    As additional information you need basics of class, object, header files etc.


  • Moderators

    @MurphaSchnee said in Why Do You Import The mainwindow.h In main.cpp?:

    By my understanding of compilation, mainwindow.cpp and main.cpp should either be run together or run sequentially, therefore, I do not understand why mainwindow.h is imported, not mainwindow.cpp. Can you explain to me why this is, and how the compilation would work?

    • main.cpp is compiled by itself to produce main.o (it doesn't need mainwindow.cpp)
    • mainwindow.cpp is compiled by itself to produce mainwindow.o (it doesn't need main.cpp)
    • After compilation has finished, the Linker connects mainwindow.o to main.o
    • main.cpp only needs mainwindow.h to know how to use mainwindow.o

    Read this to understand more: https://www.cprogramming.com/compilingandlinking.html



  • @JKSH That solved my question, thank you!


Log in to reply