How to add widgets when program is already running?
I'm trying to do some basic game that's about adding, using and deleting widgets. My problem is that I can only create widgets at program startup, if I'd like to restart the game (without closing application), the newly created widgets aren't appearing.
The QGridLayout solves that problem but creates another, manages widget positions itself, and I don't want that.
So is there anyway to create widget when program is already running (without need of adding widget to layout)?
Re: How to add widgets when program is already running?
if you are using a layout then you have to insert the widgets in the layout...
otherwise you can just call
widget = new QWidget(this)
widget->move();
any time to add a widget.. i dont understand whats the big deal in this?
Re: How to add widgets when program is already running?
Indeed, you don't understand.
Code:
void UserInterface::addButtons()
{
for (quint8 x = 1; x <= 10; x++) {
for (quint8 y = 1; y <= 10; y++) {
button->setGeometry(x * 30, y * 30, 30, 30);
buttonList.push_back(button);
}
}
}
If I call this function from constructor it's all good, everything works. But if I call it from a slot function (at random time) then widgets doesn't appear on the main window. Do you see my problem now?
Re: How to add widgets when program is already running?
just query ...
"buttonList" did u declare it globally .. how u declare it ..
Re: How to add widgets when program is already running?
Code:
QList <QPushButton
*> buttonList;
It's just to hold the address of the widgets, it's not important in that example.
Re: How to add widgets when program is already running?
so u are not adding the widget to base class QWidget layout .. try to add that widget u are storing in QList to your base ..
Re: How to add widgets when program is already running?
You don't understand either.
The widgets are created when I call that function from constructor of the class.
The widgets aren't created when I call that function via slot function.
The buttonList is not important at all. It just holds the address of the widgets so later I can access them and do whatever I want to do with them. I could simply leave that line alone but how would I delete some widgets later on?
Re: How to add widgets when program is already running?
button->show() ?
In the costructor you don't need becouse the window show is propagated for every childreen...
Works?
Re: How to add widgets when program is already running?
Yes, button->show() solves my problem, thanks.
Re: How to add widgets when program is already running?
If you want to replace the old buttons with new ones, remember to delete any previous buttons you are not using anymore. Otherwise you will have memory leaks (the buttons will take memory that is allocated but can't be accesed and is useless). The best solution would be to try to reuse the buttons or keep a list of pointers to the buttons and use qDeleteAll(). But if all the buttons are children of your main widget and you don't use any other buttons in it, you may use this:
Code:
foreach
(QObject* child, children
()) { if(child->metaObject()->className() == "QPushButton")
child->deleteLater();
}