PDA

View Full Version : How to add widgets when program is already running?



RSX
19th August 2009, 21:26
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)?

nish
20th August 2009, 04:30
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?

RSX
20th August 2009, 08:49
Indeed, you don't understand.


void UserInterface::addButtons()
{
for (quint8 x = 1; x <= 10; x++) {
for (quint8 y = 1; y <= 10; y++) {
QPushButton* button = new QPushButton(this);
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?

wagmare
20th August 2009, 09:08
just query ...
"buttonList" did u declare it globally .. how u declare it ..

RSX
20th August 2009, 10:49
QList <QPushButton*> buttonList;
It's just to hold the address of the widgets, it's not important in that example.

wagmare
20th August 2009, 11:17
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 ..

RSX
20th August 2009, 12:15
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?

PaceyIV
23rd August 2009, 12:06
button->show() ?

In the costructor you don't need becouse the window show is propagated for every childreen...

Works?

RSX
23rd August 2009, 12:34
Yes, button->show() solves my problem, thanks.

victor.fernandez
24th August 2009, 12:20
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:



foreach(QObject* child, children()) {
if(child->metaObject()->className() == "QPushButton")
child->deleteLater();
}