URGENT : How to Show Another Window From MainWindow in QT using c++
-
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... -
@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
-
@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...
-
@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
-
Mainwindow.cpp
-
main.cpp
-
chessboard.cpp (where the segmentation fault occured)
-
-
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.
-
@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 . -
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.
-
@SGaist You were right . It had something to do with the instance but when i removed the extern this happened
- chessboard.cpp
However when i kept it and added this line in my main.cpp like this
- main.cpp
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 ..)
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** - chessboard.cpp
-
@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(); -
@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
Is it possible that it needs to be inside the QApplication Class ?
- Main.cpp
-
@Jasmin-Quinn ok In that case,
game = new Game(); // in main.cppIn 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-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-Quinn move the declaration of
Game *game to MainWindow.h as global declaration -
This post is deleted!
-
@Jasmin-Quinn I hope Game *game declared in MainWindow.h only..
-
@Jasmin-Quinn paste MainWindow.h code here
Hope it's having below statements in the beginning
#ifdef MAINWINDOW_H
#define MAINWINDOW_H