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. URGENT : How to Show Another Window From MainWindow in QT using c++
Forum Updated to NodeBB v4.3 + New Features

URGENT : How to Show Another Window From MainWindow in QT using c++

Scheduled Pinned Locked Moved Solved General and Desktop
qt creator
42 Posts 6 Posters 12.1k 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.
  • nageshN nagesh

    @Jasmin-Quinn Solution as per the existing implementation of chessboard application (Just to make the integration works fine)
    1)Copy the required files to the existing project.
    2)In Mainwindow.cpp create the global game object pointer;
    Game *game; //I discourage using global, suggest only to make it work
    3)In on_lancer_partie_clicked() create the instance of it
    game = new Game();
    game->show();
    game->displayMainMenu();

    Jasmin QuinnJ Offline
    Jasmin QuinnJ Offline
    Jasmin Quinn
    wrote on last edited by
    #25

    @nagesh Hi,
    I already have all the files in my project and i created the instance on on_lancer_partie_clicked() . As for creating a global game object pointer in Minwindow.cpp it crashes .
    However , when i implement it in my main.cpp like this , it works .
    The only problem is that i need it to work when i click on the button .

    • Main.cpp
      main.cpp.png

    Is it possible that it needs to be inside the QApplication Class ?

    1 Reply Last reply
    0
    • nageshN Offline
      nageshN Offline
      nagesh
      wrote on last edited by
      #26

      @Jasmin-Quinn ok In that case,
      game = new Game(); // in main.cpp

      In button handler on_lancer_partie_clicked() write below code
      game->show();
      game->displayMainMenu();

      Note:make sure MainWindow.h should not have any game variable as member variable. It should have access to global game object pointer

      Jasmin QuinnJ 1 Reply Last reply
      0
      • nageshN nagesh

        @Jasmin-Quinn ok In that case,
        game = new Game(); // in main.cpp

        In button handler on_lancer_partie_clicked() write below code
        game->show();
        game->displayMainMenu();

        Note:make sure MainWindow.h should not have any game variable as member variable. It should have access to global game object pointer

        Jasmin QuinnJ Offline
        Jasmin QuinnJ Offline
        Jasmin Quinn
        wrote on last edited by Jasmin Quinn
        #27

        @nagesh I removed Game * game; from mainwindow.h
        and ran it but didn't work
        39bf2436-b336-4af0-ae64-5ee3c62cfe6a-image.png
        it only works when i add Game *game in either on_lancer_partie_clicked() or in mainwindow.h eventhough it already exists in main.cpp

        1 Reply Last reply
        0
        • nageshN Offline
          nageshN Offline
          nagesh
          wrote on last edited by
          #28

          @Jasmin-Quinn you are not following as suggested..

          Decide where do you want to have the instance of game.

          Follow only the steps 1)2)3) suggested earlier and make sure main is not having any game object pointer and
          MainWindow.h also should not have any game*.

          Jasmin QuinnJ 1 Reply Last reply
          0
          • nageshN nagesh

            @Jasmin-Quinn you are not following as suggested..

            Decide where do you want to have the instance of game.

            Follow only the steps 1)2)3) suggested earlier and make sure main is not having any game object pointer and
            MainWindow.h also should not have any game*.

            Jasmin QuinnJ Offline
            Jasmin QuinnJ Offline
            Jasmin Quinn
            wrote on last edited by
            #29

            @nagesh I can't do that . Without the game object pointer in main.cpp it generates this error
            21dd475e-3a65-4192-bdc8-7a4ef91fb0fa-image.png

            1 Reply Last reply
            0
            • nageshN Offline
              nageshN Offline
              nagesh
              wrote on last edited by
              #30

              @Jasmin-Quinn move the declaration of
              Game *game to MainWindow.h as global declaration

              Jasmin QuinnJ 2 Replies Last reply
              0
              • nageshN nagesh

                @Jasmin-Quinn move the declaration of
                Game *game to MainWindow.h as global declaration

                Jasmin QuinnJ Offline
                Jasmin QuinnJ Offline
                Jasmin Quinn
                wrote on last edited by
                #31
                This post is deleted!
                1 Reply Last reply
                0
                • nageshN nagesh

                  @Jasmin-Quinn move the declaration of
                  Game *game to MainWindow.h as global declaration

                  Jasmin QuinnJ Offline
                  Jasmin QuinnJ Offline
                  Jasmin Quinn
                  wrote on last edited by
                  #32

                  @nagesh Done
                  c8cf47ae-f4cf-4163-ad0c-d14158c5d82f-image.png

                  1 Reply Last reply
                  0
                  • nageshN Offline
                    nageshN Offline
                    nagesh
                    wrote on last edited by
                    #33

                    @Jasmin-Quinn I hope Game *game declared in MainWindow.h only..

                    Jasmin QuinnJ 1 Reply Last reply
                    0
                    • nageshN nagesh

                      @Jasmin-Quinn I hope Game *game declared in MainWindow.h only..

                      Jasmin QuinnJ Offline
                      Jasmin QuinnJ Offline
                      Jasmin Quinn
                      wrote on last edited by
                      #34

                      @nagesh It is i promise . Is it nomral that my compiler couldn't find the moc_mainwindow.cpp file tho ?

                      1 Reply Last reply
                      0
                      • nageshN Offline
                        nageshN Offline
                        nagesh
                        wrote on last edited by
                        #35

                        @Jasmin-Quinn paste MainWindow.h code here
                        Hope it's having below statements in the beginning
                        #ifdef MAINWINDOW_H
                        #define MAINWINDOW_H

                        Jasmin QuinnJ 1 Reply Last reply
                        0
                        • nageshN nagesh

                          @Jasmin-Quinn paste MainWindow.h code here
                          Hope it's having below statements in the beginning
                          #ifdef MAINWINDOW_H
                          #define MAINWINDOW_H

                          Jasmin QuinnJ Offline
                          Jasmin QuinnJ Offline
                          Jasmin Quinn
                          wrote on last edited by
                          #36

                          @nagesh ```
                          #ifndef MAINWINDOW_H
                          #define MAINWINDOW_H
                          #include "joueur.h"
                          #include "partie.h"
                          #include "game.h"
                          #include "qcustomplot.h"
                          #include "notepad.h"
                          #include "camera.h"
                          #include "qrplusplus.h"
                          #include <QPropertyAnimation>

                          #include <QMainWindow>

                          QT_BEGIN_NAMESPACE
                          namespace Ui { class MainWindow; }
                          QT_END_NAMESPACE

                          Game *game;

                          class MainWindow : public QMainWindow
                          {
                          Q_OBJECT

                          public:
                          MainWindow(QWidget *parent = nullptr);
                          ~MainWindow();

                          private slots:
                          void on_ajouter_joueur_clicked();

                          void on_supprimer_joueur_clicked();
                          
                          void on_tab_afficher_joueur_activated(const QModelIndex &index);
                          
                          void on_modifier_joueur_clicked();
                          
                          void on_ajouter_partie_clicked();
                          
                          void on_tab_afficher_partie_activated(const QModelIndex &index);
                          
                          void on_supprimer_partie_clicked();
                          
                          void on_modifier_partie_clicked();
                          
                          void on_chercher_joueur_clicked();
                          
                          void on_chercher_partie_clicked();
                          
                          void on_trier_joueur_clicked();
                          
                          void on_trier_partie_clicked();
                          
                          void on_imprimer_partie_clicked();
                          
                          void on_excel_joueur_clicked();
                          
                          void showTime();
                          
                          void on_envoyer_mail_released();
                          
                          void on_afficher_tout_joueurs_clicked();
                          
                          void on_afficher_tout_parties_clicked();
                          
                          void on_stat_joueur_clicked();
                          
                          void on_notes_clicked();
                          
                          
                          
                          void on_ajouter_photo_j_clicked();
                          
                          void on_prendre_photo_clicked();
                          
                          void on_qrcode_clicked();
                          
                          void on_lancer_partie_clicked();
                          

                          private:
                          Ui::MainWindow *ui;
                          Joueur J;
                          Partie P;

                          Notepad *notepad;
                          Camera *camera;
                          QRplusplus *qrcode;
                          QPropertyAnimation * animation;
                          QCustomPlot *customPlot;
                          
                          
                          QString fileNameImage ;
                          QPixmap photo ;
                          QByteArray image;
                          QPixmap pixmap;
                          QByteArray array;
                          

                          };
                          #endif // MAINWINDOW_H

                          1 Reply Last reply
                          0
                          • nageshN Offline
                            nageshN Offline
                            nagesh
                            wrote on last edited by
                            #37

                            @Jasmin-Quinn okay fine, I repeat that my first post is the solution to this application integration.

                            I repost the working solution. (I tried integrating github project which is working fine )

                            Solution as per the existing implementation of chessboard application (Just to make the integration works fine)
                            1)Copy the required files(chess project files) to the existing project.

                            2)In Mainwindow.cpp create the global game object pointer;
                            Game *game=nullptr;
                            //make sure Game *game; exists only in the Mainwindow.cpp
                            //Earlier crash was because there were two instance game * one in Manwindow.h as member variable and other with main.cpp.

                            3)In Mainwindow.cpp include the game.h file
                            #include "game.h"

                            4)In on_lancer_partie_clicked() create the instance of it
                            game = new Game();
                            game->show();
                            game->displayMainMenu();

                            Note: Assumption that github code remains unaltered.

                            A Jasmin QuinnJ 2 Replies Last reply
                            1
                            • A Offline
                              A Offline
                              Anonymous_Banned275
                              wrote on last edited by
                              #38

                              So basically back to square one.
                              I still believe that SUB_DIRS would make life easier.
                              ( adding game code from github was a snap - but it works as independent project only.

                              The qmake / management part of SUB_DIRS is OK, however, it definitely requires better knowledge of "make" ( am I repeating myself here?).

                              There are many options in qmake and it will just take some time to figure out how to

                              have (main) project
                              and be able to select real sub-project.

                              I have done that on "C level " - using MDI / QProcess and I'll try to get same result
                              using qmake.

                              1 Reply Last reply
                              0
                              • nageshN nagesh

                                @Jasmin-Quinn okay fine, I repeat that my first post is the solution to this application integration.

                                I repost the working solution. (I tried integrating github project which is working fine )

                                Solution as per the existing implementation of chessboard application (Just to make the integration works fine)
                                1)Copy the required files(chess project files) to the existing project.

                                2)In Mainwindow.cpp create the global game object pointer;
                                Game *game=nullptr;
                                //make sure Game *game; exists only in the Mainwindow.cpp
                                //Earlier crash was because there were two instance game * one in Manwindow.h as member variable and other with main.cpp.

                                3)In Mainwindow.cpp include the game.h file
                                #include "game.h"

                                4)In on_lancer_partie_clicked() create the instance of it
                                game = new Game();
                                game->show();
                                game->displayMainMenu();

                                Note: Assumption that github code remains unaltered.

                                A Offline
                                A Offline
                                Anonymous_Banned275
                                wrote on last edited by
                                #39

                                @nagesh said in URGENT : How to Show Another Window From MainWindow in QT using c++:

                                @Jasmin-Quinn okay fine, I repeat that my first post is the solution to this application integration.

                                I repost the working solution. (I tried integrating github project which is working fine )

                                Solution as per the existing implementation of chessboard application (Just to make the integration works fine)
                                1)Copy the required files(chess project files) to the existing project.

                                2)In Mainwindow.cpp create the global game object pointer;
                                Game *game=nullptr;
                                //make sure Game *game; exists only in the Mainwindow.cpp
                                //Earlier crash was because there were two instance game * one in Manwindow.h as member variable and other with main.cpp.

                                3)In Mainwindow.cpp include the game.h file
                                #include "game.h"

                                4)In on_lancer_partie_clicked() create the instance of it
                                game = new Game();
                                game->show();
                                game->displayMainMenu();

                                Note: Assumption that github code remains unaltered.

                                If I borrow Qt "cALLBACK" terminology you probably want
                                main window event loop
                                game project event loop

                                or in human -
                                run game project event loop INSIDE main project event loop
                                or in "pseudocode " (no buttons pushing )

                                int main(int argc, char *argv[])
                                {
                                QApplication main ptoject (argc, argv); main project event loop
                                QApplication game project (argc, argv); game project event loop
                                game = new Game();
                                game->show();
                                game->displayMainMenu();
                                return game project .exec();
                                return main project .exec();
                                }

                                1 Reply Last reply
                                0
                                • nageshN Offline
                                  nageshN Offline
                                  nagesh
                                  wrote on last edited by
                                  #40

                                  @AnneRanch AFAIK one instance of QApplication & one event loop required in main

                                  1 Reply Last reply
                                  1
                                  • nageshN nagesh

                                    @Jasmin-Quinn okay fine, I repeat that my first post is the solution to this application integration.

                                    I repost the working solution. (I tried integrating github project which is working fine )

                                    Solution as per the existing implementation of chessboard application (Just to make the integration works fine)
                                    1)Copy the required files(chess project files) to the existing project.

                                    2)In Mainwindow.cpp create the global game object pointer;
                                    Game *game=nullptr;
                                    //make sure Game *game; exists only in the Mainwindow.cpp
                                    //Earlier crash was because there were two instance game * one in Manwindow.h as member variable and other with main.cpp.

                                    3)In Mainwindow.cpp include the game.h file
                                    #include "game.h"

                                    4)In on_lancer_partie_clicked() create the instance of it
                                    game = new Game();
                                    game->show();
                                    game->displayMainMenu();

                                    Note: Assumption that github code remains unaltered.

                                    Jasmin QuinnJ Offline
                                    Jasmin QuinnJ Offline
                                    Jasmin Quinn
                                    wrote on last edited by
                                    #41

                                    @nagesh IT WORKKKSSS Thank youu !! How did i not think of that

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      Anonymous_Banned275
                                      wrote on last edited by
                                      #42

                                      Is it correct to say that now the "main project" has all the code of "chess" added so basically there is only one application running?

                                      The objective to "have two windows" has been met, however, adding more "child applications" using same philosophy of "cut and paste" is not what " reusing code " means to me.

                                      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