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 9.4k 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #15
    1. Add #include <QDebug> to your main cpp
    2. Add 3 qDebug line of code to constructor
    3. Add break point @ line 12 - left click on line number (get red dot)
    4. click "F5" to start in "debug" mode
    5. select #3 on bottom status bar - "Application output" to observe qDebug output
    6. click "F10 " to step into next code line
      etc etc .....

    That is just about the minimal setup to use QDebug.
    Of course there are many more options - including observing variables .

    But that will be covered in next lesson... ( just kidding , for now )

    b2e2275b-6a93-4a28-9ef2-e4e0d8b5fa6b-image.png

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

      I have added variable int a .
      Now when you "step thru" the code you can observe changes in a.
      Cool ?
      So if the variable does not have value expected....it is usually the coder's fault...

      bb0cb5ba-a62c-4e17-8aff-4c21bcd91467-image.png

      Jasmin QuinnJ 1 Reply Last reply
      1
      • A Anonymous_Banned275

        I have added variable int a .
        Now when you "step thru" the code you can observe changes in a.
        Cool ?
        So if the variable does not have value expected....it is usually the coder's fault...

        bb0cb5ba-a62c-4e17-8aff-4c21bcd91467-image.png

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

        @AnneRanch Hi !Thank you for your patience and time .
        I followed your steps and this is what i got when i clicked on the button
        Capture.PNG

        jsulmJ 1 Reply Last reply
        0
        • Jasmin QuinnJ Jasmin Quinn

          @AnneRanch Hi !Thank you for your patience and time .
          I followed your steps and this is what i got when i clicked on the button
          Capture.PNG

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

          @Jasmin-Quinn Your "game" pointer seems to be dangling pointer (not pointing to a valid instance). So, make sure game is pointing to a Game instance...

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

          Jasmin QuinnJ 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Jasmin-Quinn Your "game" pointer seems to be dangling pointer (not pointing to a valid instance). So, make sure game is pointing to a Game instance...

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

            @jsulm I don't understand what should I change ! I know the code works pretty fine on its own then i must've made a mistake when calling the game in my project.

            • Mainwindow.h
              mainwh.png

            • Mainwindow.cpp
              mainwincpp.png

            • main.cpp
              maincpp.png

            • chessboard.cpp (where the segmentation fault occured)
              chessboard.png

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #20

              Hi,

              You have static game pointer in your main.cpp file that is never initialized and that is the one used in the Chessboard class.

              However, the architecture is wrong. Why would you need a static Game object created somewhere and then all of a sudden the Chessboard class in need to know about it using extern ?

              If Chessboard really needs Game then make that relation explicit.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Jasmin QuinnJ 1 Reply Last reply
              1
              • SGaistS SGaist

                Hi,

                You have static game pointer in your main.cpp file that is never initialized and that is the one used in the Chessboard class.

                However, the architecture is wrong. Why would you need a static Game object created somewhere and then all of a sudden the Chessboard class in need to know about it using extern ?

                If Chessboard really needs Game then make that relation explicit.

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

                @SGaist Hi,
                i found the game source code in github and i wanted to integrate it in my project . And since it works pretty fine on its own so i figured i did a mistake in either main.cpp or mainwindow.cpp . I actually don't even know what using extern stands for .

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #22

                  You should read about it.

                  In any case, it's not the right way to do it. You can remove the instance you have in your widget but do not forget to instanciate the one you have in your main.cpp file.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Jasmin QuinnJ 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    You should read about it.

                    In any case, it's not the right way to do it. You can remove the instance you have in your widget but do not forget to instanciate the one you have in your main.cpp file.

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

                    @SGaist You were right . It had something to do with the instance but when i removed the extern this happened

                    • chessboard.cpp
                      remove.png

                    However when i kept it and added this line in my main.cpp like this

                    • main.cpp
                      instance.png

                    it didn't crash and it showed this window, which means it couldn't load the chessboard or the ressources (in the middle of the game such as the pawn, queen ..)
                    exec.png
                    exec2.png

                    and as an OUTPUT :
                    DEBUG TRACE MainWindow::MainWindow(QWidget) @ line # 42
                    DEBUG TRACE MainWindow::MainWindow(QWidget
                    ) @ line # 43
                    DEBUG TRACE MainWindow::MainWindow(QWidget*) @ line # 44
                    DirectWrite: CreateFontFaceFromHDC() failed (Indique une erreur dans un fichier d’entrée, tel qu’un fichier de polices.) for QFontDef(Family="", pointsize=18, pixelsize=24, styleHint=5, weight=50, stretch=100, hintingPreference=0) LOGFONT("MS Sans Serif", lfWidth=0, lfHeight=-24) dpi=96**

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

                      @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 1 Reply Last reply
                      0
                      • 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

                                          • Login

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