URGENT : How to Show Another Window From MainWindow in QT using c++
-
@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 -
@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_NAMESPACEGame *game;
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
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 -
@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.
-
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.