Replacing widget pointer in layout
-
wrote on 5 Oct 2019, 18:35 last edited by
I have a Widget which i created in the constructor and placed into a layout.
Game::Game(QWidget *parent) : QWidget(parent), //... mMinefield{ new Minefield}, //... { auto topLayout = new QHBoxLayout; //.. other widgets get added here mTopFrame->setLayout(topLayout); auto bottomLayout = new QHBoxLayout; bottomLayout->addWidget(mMinefield); mBottomFrame->setLayout(bottomLayout); auto mainLayout = new QVBoxLayout; mainLayout->addWidget(mTopFrame); mainLayout->addWidget(mBottomFrame); mMainFrame->setLayout(mainLayout); auto layout = new QVBoxLayout; layout->addWidget(mMainFrame); setLayout(layout); }
Now at some point i want to delete this
Minefield
and replace it with a new one:I tryed this:
void Game::makeNewMinefield() { delete mMinefield; mMinefield = new Minefield; }
The result is the old widget is gone but i don't see a new one. What is the correct way here to replace the widget?
-
Hi,
Set your widget in the layout.
-
wrote on 5 Oct 2019, 18:46 last edited by
What do you mean with that?
-
For example use replaceWidget.
-
wrote on 5 Oct 2019, 18:49 last edited by
I tryed this:
void Game::makeNewMinefield() { auto newMinefield = new Minefield; auto returnItem = layout()->replaceWidget(mMinefield, newMinefield); if(returnItem) { qDebug() << "success"; } else { qDebug() << "fail"; } mMinefield = newMinefield; }
but i get fail. It seems like he cant find the old widget event if its already placed.
-
Silly question but are you sure you have that widget set in the first place ?
-
wrote on 5 Oct 2019, 18:58 last edited by
I have an idea why its not working.
I put it in a layout in the constructor:
bottomLayout->addWidget(mMinefield);
Then it gets into a QFrame:
mBottomFrame->setLayout(bottomLayout);
Then the two frames i have get played into annother layout
mainLayout->addWidget(mTopFrame); mainLayout->addWidget(mBottomFrame);
This Layout gets played into annotther frame:
mMainFrame->setLayout(mainLayout);
this frame gets played in annother layout which is the widgets layout:
layout->addWidget(mMainFrame); setLayout(layout);
So i guess i have to look for the layout in the frame?
-
wrote on 5 Oct 2019, 19:01 last edited by
I could replace it like this:
void Game::makeNewMinefield() { auto newMinefield = new Minefield{ mDefaultFieldWidth, mDefaultFieldHeight, mDefaultMines }; auto returnItem = mBottomFrame->layout()->replaceWidget( mMinefield, newMinefield); if(returnItem) { qDebug() << "success"; } else { qDebug() << "fail"; } mMinefield = newMinefield; }
It seems to work.
Is this the correct way then? What should i do withreturnItem
in real code? -
Delete it since you won't use its content.
-
wrote on 5 Oct 2019, 19:15 last edited by
Ok I think the issue is solved. Again something learned.
void Game::makeNewMinefield() { auto newMinefield = new Minefield{ mDefaultFieldWidth, mDefaultFieldHeight, mDefaultMines }; auto returnItem = mBottomFrame->layout()->replaceWidget( mMinefield, newMinefield); if(!returnItem) { QMessageBox::critical(this, tr("Internal Error"), tr("Minefield could not be replaced"), QMessageBox::Close); qApp->quit(); } delete returnItem; mMinefield = newMinefield; }
1/10