PDA

View Full Version : adding objects



ashboi
30th May 2012, 15:08
I'm currenting writing a mastermind game. currently i'm having problems on the pushbutton for new game.
there is a spinbox for the secretcode length. and this would send a signal to the secretcode frame slot telling it what length the user wants and when the new game button is pressed the default code would be deleted and the new secret code legth would be generated.
I've managed to delete the default code, but cant seem to go about adding back the peg objects for the new length.
I'm getting QLayout: Attempting to add QLayout "" to Secret "", which already has a layout in my terminal whenever i click on new game button. i'm thinking this has to do with me trying to add another layout for the new peg objects when the new game is pressed, but cant seem to figure out how to change this.


heres my code

Secret::Secret(QWidget *parent,int j): QWidget(parent), currentLength(j)
{
QHBoxLayout *layout= new QHBoxLayout(this);
for(int i = 0; i<currentLength; i++)
{
Peg *peg= new Peg(this);
pegv.push_back(peg);
layout->addWidget(peg);
}
}

void Secret::changeCode(const QString x)
{
currentLength=x.toInt();
}

void Secret::newGame()
{
while(pegv.size()>0)
{
Peg* temp=pegv.back();
delete temp;
pegv.pop_back();
}
QHBoxLayout *layout= new QHBoxLayout(this);
for(int i = 0; i<currentLength; i++)
{
Peg *peg= new Peg(this);
pegv.push_back(peg);
layout->addWidget(peg);
}

}

amleto
30th May 2012, 19:34
this->setLayout(0); // I'm guessing that this will remove the previous layout.



fyi: you shouldn't be deleteing your peg widgets like that. Use deleteLater() instead.

ashboi
31st May 2012, 09:49
this->setLayout(0);
is giving me this error when i press new game.
QWidget::setLayout: Cannot set layout to 0
QLayout: Attempting to add QLayout "" to Secret "", which already has a layout

I'm wondering if I can just delete the pegs but use the same layout to add new pegs to it?