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. main.cpp Vs mainwindow.cpp
Forum Updated to NodeBB v4.3 + New Features

main.cpp Vs mainwindow.cpp

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 3.0k Views 2 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.
  • Swati777999S Swati777999

    Hi All,

    I am new to Qt and feel that I've to learn a lot. I am learning through various online resources and youtube Tutorials and often get stuck in many positions. So please bear me for my obvious doubts/queries.

    While creating any project in Qt Creator, 2 source files such as main.CPP and mainwindow.cpp are created. main.cpp contains the declaration of the main class, functions, signal and slots , and command for displaying in the screen whereas mainwindow.cpp contains the implementation of these functions and signal and slots [Correct me if I'm wrong].

    Is there any specific reason why it is done so?

    Thanks in advance!

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Swati777999 said in main.cpp Vs mainwindow.cpp:

    main.cpp contains the declaration of the main class, functions, signal and slots , and command for displaying in the screen whereas mainwindow.cpp contains the implementation of these functions and signal and slots [Correct me if I'm wrong].

    Yes, your description is wrong. main.cpp (created in QtCreator) does not contain any class declarations (there is also no "main" class, you probably mean main function, you should use correct wording to avoid misunderstandings).

    "Is there any specific reason why it is done so?" - yes, plain old modularity. You usually don't put all your code into one file, right? Normally you have one header file and cpp file for ech class, simply to make your code easier to understand and maintain. main.cpp is something each application usually have: it contains the main() function which is the entry point of a C or C++ application. Though you don't have to name it main.cpp, file name does not matter, main.cpp is just a convention.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    Swati777999S 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Swati777999 said in main.cpp Vs mainwindow.cpp:

      main.cpp contains the declaration of the main class, functions, signal and slots , and command for displaying in the screen whereas mainwindow.cpp contains the implementation of these functions and signal and slots [Correct me if I'm wrong].

      Yes, your description is wrong. main.cpp (created in QtCreator) does not contain any class declarations (there is also no "main" class, you probably mean main function, you should use correct wording to avoid misunderstandings).

      "Is there any specific reason why it is done so?" - yes, plain old modularity. You usually don't put all your code into one file, right? Normally you have one header file and cpp file for ech class, simply to make your code easier to understand and maintain. main.cpp is something each application usually have: it contains the main() function which is the entry point of a C or C++ application. Though you don't have to name it main.cpp, file name does not matter, main.cpp is just a convention.

      Swati777999S Offline
      Swati777999S Offline
      Swati777999
      wrote on last edited by
      #3

      @jsulm Thanks for the clarification.

      While creating a new project, I just go blank about where[in which files] to write the function declaration or definition when the functions are declared as the member of the "main" class.

      I want to create a basic application for displaying a message when the button is clicked without using .ui file/form file. I am confused about the location of writing functions.

      Any help will be appreciated!

      Thanks in help!

      “ In order to be irreplaceable, one must always be different” – Coco Chanel

      artwawA jsulmJ 2 Replies Last reply
      0
      • Swati777999S Swati777999

        @jsulm Thanks for the clarification.

        While creating a new project, I just go blank about where[in which files] to write the function declaration or definition when the functions are declared as the member of the "main" class.

        I want to create a basic application for displaying a message when the button is clicked without using .ui file/form file. I am confused about the location of writing functions.

        Any help will be appreciated!

        Thanks in help!

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by artwaw
        #4

        @Swati777999 said in main.cpp Vs mainwindow.cpp:

        basic application for displaying a message when the button is clicked without using .ui file/form file

        No, you don't. For basic thing you want to use .ui file, trust me. It's the simplest approach. Is there a reason you want to dive into creating the window yourself?

        You should read the documentation and study basic examples, truth to tell. Let me see if I can break it down for you:

        • main.cpp For basic things you described you really don't want to do anything with it. Forget it exists.
        • MainWindow.cpp/MainWindow.h - this is your main window class, together with MainWindow.ui that describes the visible part. Think of it as your main program class in which all stuff happens. For what you described, you'd need to go to the design mode, place and name your button. Then in the header you'd need to define the slot (which really is just a void method). In cpp file you'd need to write what that method does: in your case it would make use, probably, of one of QMessageBox static methods for displaying the message dialog. Then you need to connect it using Signals and Slots mechanism (check what it is in the documentation) using connect() method.

        I would provide the links to the documentation but I don't know which version of Qt you use - but you can easily access all the help online or via QtCreator. Qt is really well documented but will not help you with basic c++ (and from what you wrote I have a feeling that you need to beef up that knowledge too), for that you can visit https://en.cppreference.com/w/

        I understand that there is a great deal of knowledge to ingest at first but, minding that Qt is absolutely fabulous when it comes to basic tutorials and overall documentation, you only need a bit of patience.

        For more information please re-read.

        Kind Regards,
        Artur

        Swati777999S 1 Reply Last reply
        4
        • Swati777999S Swati777999

          @jsulm Thanks for the clarification.

          While creating a new project, I just go blank about where[in which files] to write the function declaration or definition when the functions are declared as the member of the "main" class.

          I want to create a basic application for displaying a message when the button is clicked without using .ui file/form file. I am confused about the location of writing functions.

          Any help will be appreciated!

          Thanks in help!

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @Swati777999 It sounds like you don't have much programming experience?
          I would suggest you learn C++ basics first, else it will be hard to do anything meaningful with Qt.

          And again: what is a "main" class? Do you mean the main function? You should understand the difference between files (main.cpp), classes (MainWindow) and functions (main()), this is fundamental.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Swati777999S 2 Replies Last reply
          5
          • jsulmJ jsulm

            @Swati777999 It sounds like you don't have much programming experience?
            I would suggest you learn C++ basics first, else it will be hard to do anything meaningful with Qt.

            And again: what is a "main" class? Do you mean the main function? You should understand the difference between files (main.cpp), classes (MainWindow) and functions (main()), this is fundamental.

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #6

            @jsulm oh, sorry for writing the class wrong, By "main" class, I referred to the "MainWindow class" declared in MainWindow.h file.

            I am familiar with basic concepts of C++ but I am literally struggling a lot for correctly using different functions and properties of various classes in Qt. Although I refer to Qt documentation for more information about these predefined functions and properties,I rarely get the output I desire.

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            1 Reply Last reply
            0
            • artwawA artwaw

              @Swati777999 said in main.cpp Vs mainwindow.cpp:

              basic application for displaying a message when the button is clicked without using .ui file/form file

              No, you don't. For basic thing you want to use .ui file, trust me. It's the simplest approach. Is there a reason you want to dive into creating the window yourself?

              You should read the documentation and study basic examples, truth to tell. Let me see if I can break it down for you:

              • main.cpp For basic things you described you really don't want to do anything with it. Forget it exists.
              • MainWindow.cpp/MainWindow.h - this is your main window class, together with MainWindow.ui that describes the visible part. Think of it as your main program class in which all stuff happens. For what you described, you'd need to go to the design mode, place and name your button. Then in the header you'd need to define the slot (which really is just a void method). In cpp file you'd need to write what that method does: in your case it would make use, probably, of one of QMessageBox static methods for displaying the message dialog. Then you need to connect it using Signals and Slots mechanism (check what it is in the documentation) using connect() method.

              I would provide the links to the documentation but I don't know which version of Qt you use - but you can easily access all the help online or via QtCreator. Qt is really well documented but will not help you with basic c++ (and from what you wrote I have a feeling that you need to beef up that knowledge too), for that you can visit https://en.cppreference.com/w/

              I understand that there is a great deal of knowledge to ingest at first but, minding that Qt is absolutely fabulous when it comes to basic tutorials and overall documentation, you only need a bit of patience.

              Swati777999S Offline
              Swati777999S Offline
              Swati777999
              wrote on last edited by
              #7

              @artwaw I agree that designing with .ui is very simple and there have tons of resources and tutorials available on the internet. I would have always preferred to experiment with .ui files only but my present assignment does not permit me to use .ui file. So, I am struggling in this part for designing something without .ui . Any resources that you would like to suggest me to simplify my learning experience?

              “ In order to be irreplaceable, one must always be different” – Coco Chanel

              Thank YouT 1 Reply Last reply
              0
              • Swati777999S Swati777999

                @artwaw I agree that designing with .ui is very simple and there have tons of resources and tutorials available on the internet. I would have always preferred to experiment with .ui files only but my present assignment does not permit me to use .ui file. So, I am struggling in this part for designing something without .ui . Any resources that you would like to suggest me to simplify my learning experience?

                Thank YouT Offline
                Thank YouT Offline
                Thank You
                wrote on last edited by
                #8

                @Swati777999

                I want to create a basic application for displaying a message when the button is clicked without using .ui file/form file. I am confused about the location of writing functions.

                #include <QApplication>
                #include <QPushButton>
                // these are the files from QT
                
                int main(int argc, char **argv) // this is function not class. And C++ application starts with this
                {
                 QApplication app (argc, argv);
                
                 QPushButton button ("Hello world !");
                 button.show();
                
                
                /* here you are creating button and showing it
                 if you want text then you can include QLabel and work with it.
                You can create signals like you create signals in other applications and connect them
                or just use QWidget. You can go with single file. And QT creator makes it easy for us to get started and make it modular as @jsulm said
                */
                
                 return app.exec();
                //When calling app.exec() the event loop is launched. 
                }
                

                QApplication is a very important class. It takes care of input arguments, but also a lot of other things, and most notably, the event loop. The event loop is a loop that waits for user input in GUI applications.

                You can see this one for getting other codes too
                https://wiki.qt.io/Qt_for_Beginners

                Let's make QT free or It will go forever

                TRUE AND FALSE <3

                1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Swati777999 It sounds like you don't have much programming experience?
                  I would suggest you learn C++ basics first, else it will be hard to do anything meaningful with Qt.

                  And again: what is a "main" class? Do you mean the main function? You should understand the difference between files (main.cpp), classes (MainWindow) and functions (main()), this is fundamental.

                  Swati777999S Offline
                  Swati777999S Offline
                  Swati777999
                  wrote on last edited by
                  #9

                  @jsulm Take this example ; Presenting data in a table view

                  As it is not mentioned which sections of these codes will go to which files; MainWindow.cpp, main.cpp, or mainwindow.h , it creates trouble for beginners. There have been so many code snippets written like the above in Qt documentation. I assume that those codes form part of the implementation of the functions.

                  “ In order to be irreplaceable, one must always be different” – Coco Chanel

                  artwawA jsulmJ 2 Replies Last reply
                  0
                  • Swati777999S Swati777999

                    @jsulm Take this example ; Presenting data in a table view

                    As it is not mentioned which sections of these codes will go to which files; MainWindow.cpp, main.cpp, or mainwindow.h , it creates trouble for beginners. There have been so many code snippets written like the above in Qt documentation. I assume that those codes form part of the implementation of the functions.

                    artwawA Offline
                    artwawA Offline
                    artwaw
                    wrote on last edited by
                    #10

                    @Swati777999 said in main.cpp Vs mainwindow.cpp:

                    As it is not mentioned which sections of these codes will go to which files; MainWindow.cpp, main.cpp, or mainwindow.h , it creates trouble for beginners.

                    That would suggest you still need to catchup on the basics of c++.

                    @Swati777999 said in main.cpp Vs mainwindow.cpp:

                    my present assignment does not permit me to use .ui

                    Everything that's done in the Designer can be coded by hand. It is a tad more difficult though.

                    As fro the sources on how to - I do believe Qt documentation covers all of that. You should start by making yourself familiar with QApplication, QMainWindow and signals and slots.

                    For more information please re-read.

                    Kind Regards,
                    Artur

                    1 Reply Last reply
                    1
                    • Swati777999S Swati777999

                      @jsulm Take this example ; Presenting data in a table view

                      As it is not mentioned which sections of these codes will go to which files; MainWindow.cpp, main.cpp, or mainwindow.h , it creates trouble for beginners. There have been so many code snippets written like the above in Qt documentation. I assume that those codes form part of the implementation of the functions.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Swati777999 said in main.cpp Vs mainwindow.cpp:

                      As it is not mentioned which sections of these codes will go to which files; MainWindow.cpp, main.cpp, or mainwindow.h , it creates trouble for beginners

                      There are tons of example Qt applications linked in Qt documentation.
                      You should really take a look...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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