Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why Do You Import The mainwindow.h In main.cpp?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MurphaSchnee
    wrote on last edited by
    #1

    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?

    JKSHJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      3
      • M Offline
        M Offline
        MurphaSchnee
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by dheerendra
          #4

          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
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          1
          • M Offline
            M Offline
            MurphaSchnee
            wrote on last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

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

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MurphaSchnee
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

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

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MurphaSchnee
                    wrote on last edited by
                    #9

                    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
                    
                    1 Reply Last reply
                    0
                    • dheerendraD Offline
                      dheerendraD Offline
                      dheerendra
                      Qt Champions 2022
                      wrote on last edited by
                      #10

                      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.

                      Dheerendra
                      @Community Service
                      Certified Qt Specialist
                      http://www.pthinks.com

                      1 Reply Last reply
                      3
                      • M MurphaSchnee

                        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?

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by JKSH
                        #11

                        @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

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        4
                        • M Offline
                          M Offline
                          MurphaSchnee
                          wrote on last edited by
                          #12

                          @JKSH That solved my question, thank you!

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved