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
-
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 = appand these on my mainwindow.cpp:
void MainWindow::on_lancer_partie_clicked()
{game = new Game();
game->show();
game->displayMainMenu();
} -
@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. -
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.
-
@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 pngsFor 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. -
@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
-
@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. -
- 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.
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.