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

main.cpp Vs mainwindow.cpp



  • 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!



  • @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.


  • Lifetime Qt Champion

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



  • @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!



  • @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.


  • Lifetime Qt Champion

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



  • @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.



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



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



  • @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.



  • @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.


  • Lifetime Qt Champion

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


Log in to reply