Solved URGENT : How to Show Another Window From MainWindow in QT using c++
The process was ended forcefully.
It means it crashed.
I can see it also has a resource.qrc
file which you also need for its graphics.
(and the PNG files also, placed in sub folder)
So i guess thats the reason for the crash.
they're all in but still same error
The path is wrong
you have 2 times images and in the original example there is only one folder
so it still can't find the pngs
For test i added to a default GUI app and it just works so
don't give up on it. its just the image path not being correct, then it should run.
make sure the PNG files are in the project folder.
(same as where your .pro file is)
in a subfolder called Images
It cannot be in any more sub folders as then path in code is wrong.
@mrjj Hello Sir, actually the second folder wasn't open so the pictures are only included in :/images
@AnneRanch Thank you Sir but am a beginner and i really looked it out but could'nt find something close to my case ! can you tell me how i can run it on click using QProcess and what should i include / add in my .pro file
I looked over the code for chess.
Do notice it has a global variable called game it uses all over the place.
You must have the same in your project.
In main.cpp it
and the other modules use that.
Did you do the same in your project?
Also, do you know how to use the debugger ? It can be used to find out what crashes.
But might be the game variable since the images now seem fine.
@Jasmin-Quinn I would suggest to "back-up " a little.
Start with "new project" -> Application (Qt) ->Qt Widget Application
Now you can add a class to your one and only project.
Then you have a simple "display window " application and its "form" and a test class - all as one project.
Add "testFunction " to your class.
In QtDesigner add "button" and use "go to slot" - click - to build "SIGNAL /SLOT"
Then you have a function where you can actually test the "click button".
Then you add debugging - #inlcude <QDebug> - and use QDebug in your "button pushed " function to output test message. .
Then let the "button pushed" test function to create an instance of your class and execute the testFunction - 'button pushed".
That is in essence what you asking , is it not ?
You have "main window" "widget" and you can create another "widget" - by simply replacing the button click "show debug message" with your other "project" / window./widget.
All of this is done letting the QtCreator /QTDesigner do the grunt work and let them implement whatever they need - .pro etc...
( From personal experience - do not mess with .pro too much)
And if you do make typo error - you get more meaningfull error message than the "project terminated...." Those are not that obvious to fix, but with peppering your code with QDebug messages you have an easier job.
When the "project terminated" happens - just use debugger , add break points and "step thru" your code until it "breaks". You may not find exact spot where the problem is, but it gets you into "hood" - "close enough for government work".
Best of luck coding.
@mrjj Hi sorry for my late reply, yes i added Game *game in both my main.cpp and mainwindow.h but still crashes . And no i don't know how to use the debugger can you help me?
- Add #include <QDebug> to your main cpp
- Add 3 qDebug line of code to constructor
- Add break point @ line 12 - left click on line number (get red dot)
- click "F5" to start in "debug" mode
- select #3 on bottom status bar - "Application output" to observe qDebug output
- 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 )
I have added variable int a .
Now when you "step thru" the code you can observe changes in a.
So if the variable does not have value expected....it is usually the coder's fault...
Jasmin Quinn last edited by 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
@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.
chessboard.cpp (where the segmentation fault occured)
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.
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.
Jasmin Quinn last edited by Jasmin Quinn
@SGaist You were right . It had something to do with the instance but when i removed the extern this happened
However when i kept it and added this line in my main.cpp like this
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**
nagesh last edited by 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();
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 .
Is it possible that it needs to be inside the QApplication Class ?
nagesh last edited by
@Jasmin-Quinn ok In that case,
game = new Game(); // in main.cpp
In button handler on_lancer_partie_clicked() write below code
Note:make sure MainWindow.h should not have any game variable as member variable. It should have access to global game object pointer