I 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 projet to the main one and i don't know how can i call it 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 My project: enter image description here
the game: enter image description here
You do not need.ui files. You just have to write the function yourself. I don't know what the main widget that owns the button is called so I'll call it TLWidget for now. and use... to represent some other code that may be in there.
#include<QtWidgets/QDialog>
#include "game.h" // this is the widget you want in a new window
class TLWidget ... //this is the class definition of your top level widget. it usually owns the thing created from the .ui
{
...
QDialog* m_ChessWindow; //a member is needed to hold or own the window
Game* m_ChessWidget; //the game itself
...
TLWidget (...) //this is the constructor (where the ui item is created or initialized)
{
...
//by making in the constructor, there's only one and it is easy to track.
//you can alternatively spawn a new window per button push by following the 3 calls here: the two new calls below with appropriate parents, and the show() call further below.
m_ChessWindow = new QDialog(this);
m_ChessWidget = new Game(m_ChessWindow);
...
}
...
void SpawnChess() //function that creates your widget. You may want to put other steps in here or slot this, or call it from your button slot function
{
...
m_ChessWindow->show();//this will show the dialog
...
}
...
};
I tried that and i did some other steps too. Here's where am at right now.
Mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include "joueur.h" #include "partie.h" #include "game.h"
#include
QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE
class MainWindow: public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
private slots: void on_ajouter_joueur_clicked();
void on_lancer_partie_clicked(); ..etc
private: Ui::MainWindow *ui; Joueur J; Partie P; * Game game;
}; #endif // MAINWINDOW_H
Mainwindow.cpp
void MainWindow::on_lancer_partie_clicked() {
game = new Game(); game->show(); game->displayMainMenu();
}
Main.cpp
#include "mainwindow.h" #include #include #include #include "game.h"
* Game game;
int main(int argc, char *argv[]) { QApplication a(argc, argv);
a.setStyle("fusion"); MainWindow w;
game = new Game();
w.show();
return a.exec(); }
At this point, a new window pops when i click on the button but the chessboard and the chess pieces are nowhere to be found: BTW i have all the files and ressources with the right path ! If i do this tho :
Main.cpp
#include "mainwindow.h" #include #include #include #include "game.h"
* Game game;
int main(int argc, char *argv[]) { QApplication a(argc, argv);
a.setStyle("fusion"); MainWindow w;
game = new Game(); game->show(); game->displayMainMenu();
w.show();
return a.exec();
This way it works pretty fine not missing a single piece. So i figured it has something to do with the QApplication but i don't know how to use QApplication in mainwindow.cpp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.