Unsolved Problem with QWidgets on GridLayout
-
Hey Guys,
I have a problem with my Gridlayout. I am using my Gridlayout to manage 2 Gameboards on my GUI. I want the User to be able to change between to Games. Both GameWidgets are already developed.
My idea was just to start one of the two games in my Constructor with:
ui->gameBoard->addWidget(&gm,0,0)
and in my Header:
GameWidget gm;
Btw my second GameWidget is
SnakeWidget sm;
So I used a ComboBox to let the User decide what he wants to play:..
gameIndex = ui->gameCombo->currentIndex(); switch(gameIndex){ case 0:ui->gameBoard->addWidget(&gm);break; case 1:ui->gameBoard->addWidget(&sm);break; }
So if I start my GUI I am not getting any Error´s I even can change my first Game to my second one. But I am not able to change the Games back. I guess it has something to do with that I dont delete the game or so..
I have really no idea what to do :D. I am still very new in Qt and C++. Maybe I should not use a GridLayout for that. Another important thing is that I have to paint on my GameWidgets or to be able to grab keyboard events or mousepressevents/click.. .
I hope you can understand my problem so far.. if not I can write anything more specific.
Sorry for my bad english :).I also tried to remove Widgets to replace Widgets but I failed ..
-
Hi,
Wouldn't a QStackedWidget be better suited for that task ?
-
Never read that word before. I will check it out definitly ! Thanks !
-
Ok wow, this one works !
How can a beginner learn to find those structures ?I got another Problem now.. I can play both games this works. But now my gui crashes when I close it :/
Debugger say something is wrong with my SnakeWidget Class is wrong.
SIGSEGV Segmentation fault.Any fast idea before I can go to bed :D?
I am getting to the point where the debugger tells me my deconstructor is failing ..?
-
Hi
its a double deletionGameWidget gm; << those are deleted by their containing class.
SnakeWidget sm;<< the layout thinks its own them and will also delete
case 0:ui->gameBoard->addWidget(&gm);break;
case 1:ui->gameBoard->addWidget(&sm);break;In Qt, objects can own each other and a parent will delete it childs.
-
Okay so I need to stop one deletion, I tried now to remove the QStackedWidgets manually in my MainWindow Destructur. As I thought it does not work.
Is it the problem that I never said, that GameWidget and SnakeWidget should be a Child of my MainWindow ?
-
@tianstar Just allocate the widgets on the stack, and do NOT delete them manually:
GameWidget *gm; SnakeWidget *sm; gm = new GameWidget(); sm = new SnakeWidget(); gameIndex = ui->gameCombo->currentIndex(); switch(gameIndex){ case 0:ui->gameBoard->addWidget(gm);break; case 1:ui->gameBoard->addWidget(sm);break; }
They will be automatically deleted when gameBoard is deleted.
-
Thank you guys for your help ! really really really great ! :)
Have a nice day ! -
One small note, you're "doing it wrong"
In your constructor:
gm = new GameWidget(); ui->gameBoard->addWidget(gm) sm = new SnakeWidget(); ui->gameBoard->addWidget(sm) connect(ui->gameCombo, qOverload<int>(QComboBox::activated), ui->gameBoard, QStackedWidget::setCurrentIndex);
There's no need to add the widgets again and again to the QStackedWidget.
If you have errors for qOverload see the corresponding documentation for its alternatives.