Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • Hello am creating a desktop application in qt using c++ and am struggling with linking two projects together. The first project is a chess game and the second one is from where i want to start . I want to add a new QPushbutton "Play a game" then a new window will open showing the game . I added all .cpp and .h file from the first project to my project and i don't know how can i call the second window . I could've done it if it had a .ui file with just setmodel but i can't because it doesn't have one. I don't know If i am making any sense right now but i'll provide you with screenshots just so you get me more. The first project (the game): https://github.com/subeshb1/Chess

    Application.png
    game.png


  • Lifetime Qt Champion

    Hi
    If you dont have name classhes between the projects, you can show the other windows
    by including its files the "target" project and then simply new and show the second window.

    Like
    game = new Game();
    game->show();
    game->displayMainMenu();



  • @Jasmin-Quinn If first project built executable is available, then you can run on click of button using "QProcess"
    https://doc.qt.io/qt-5/qprocess.html



  • @mrjj
    OUTPUT: 01:41:08: The process was ended forcefully.

    I added these lines in my .pro file:
    TARGET = Chess
    TEMPLATE = app

    and these on my mainwindow.cpp:
    void MainWindow::on_lancer_partie_clicked()
    {

    game = new Game();
    game->show();
    game->displayMainMenu();
    }



  • @nagesh Eventhough i prefer to include them all in one project can you provide me with an example of how i can run it on click using QProcess because i looked it out and couldn't find something helpful



  • @Jasmin-Quinn said in URGENT : How to Show Another Window From MainWindow in QT using c++:

    linking two projects together.

    I am not sure if I understand the above.

    Let me say this - SUB_DIRS concept is to manage multiple INDEPENDENT project. By manage I mean they are NOT running as ONE project "out of the box".

    However, with some effort they can all work as a single project.
    ( I do use QProcess)
    I am deliberately avoiding using term "linked together" - that should be reserved for real "linker".

    Utilizing SUB_DIRS concept I am using "MDI" as top project and executing SUB_DIRS "subprojects" by using "windows standard " menu driven "actions" coded in "MDI" .

    So if I understand your task to be able "link one project from another" -
    in my case "main controlling project " is "MDI" and one of my "other projects" is "scan for local Bluetooth devices"....
    The SUB_DIRS used this way may sound too convoluted - but it does aid in code reusing.


  • Lifetime Qt Champion

    @Jasmin-Quinn

    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)

    alt text

    So i guess thats the reason for the crash.



  • @mrjj Capture.PNG
    they're all in but still same error


  • Lifetime Qt Champion

    @Jasmin-Quinn
    Hi
    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 :/imagesCapture.PNG



  • @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


  • Lifetime Qt Champion

    @Jasmin-Quinn
    Hi
    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
    has
    Game *game;

    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
    plain "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?



    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



  • 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



  • @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


  • Lifetime Qt Champion

    @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
      mainwh.png

    • Mainwindow.cpp
      mainwincpp.png

    • main.cpp
      maincpp.png

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


  • Lifetime Qt Champion

    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 .


  • Lifetime Qt Champion

    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
      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**



  • @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
      main.cpp.png

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



  • @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



  • @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



  • @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*.



  • @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



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



  • This post is deleted!


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



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



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



  • @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_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



  • @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.



  • @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();
    }



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


Log in to reply