I have a code for generating card items to the Qt scene. This is what I came up with so far. The member functions called are just what you would derive from the name, so I don't need to include them here.
// creating the cards
int x = 0;
int y = 0;
int line_brake = 0;
for(int i = 0; i < width; i++)
{
for(int j = 0; j < width; j++)
{
card = new Card();
int card_width = card->getWidth();
int card_height = card->getHeight();
if(j == (width-1))
{
line_brake = 1;
}
else if((j != (width-1)) && (line_brake == 1))
{
y += card_height;
card->setPos(x,y);
line_brake = 0;
x = 0 - card_width;
}
else
{
card->setPos(x,y);
x += card_width;
}
scene->addItem(card);
}
}
This is how my scene looks after this code runs:
What might be the problem? I need the cards to be layed out in a 7*7 square. That means 7 rows, 7 columns and in each field a card image.
This is how my scene looks after edit suggestions by @molbdnilo:
// creating the cards
int x = 0;
int y = 0;
int line_brake = 0;
for(int i = 0; i < width; i++)
{
for(int j = 0; j < width; j++)
{
card = new Card();
int card_width = card->getWidth();
int card_height = card->getHeight();
if(j == (width-1))
{
line_brake = 1;
continue;
}
else if((j != (width-1)) && (line_brake == 1))
{
y += card_height;
card->setPos(x,y);
line_brake = 0;
x = 0;
}
else
{
card->setPos(x,y);
x += card_width;
}
scene->addItem(card);
}
}
To be honest, I don't know why this code makes problems. I also don't know why it is written in such a complex way. To generate the cards I would do the following:
const int size = 7;
for(int row = 0; row < size; row++)
{
for(int col = 0; col < size; col++)
{
Card *card = new Card;
int card_width = card->getWidth();
int card_height = card->getHeight();
int x = col * card_width;
int y = row * card_height;
card->setPos(x, y);
scene->addItem(card);
}
}
Even more. According to the picture, it looks like all cards have the same dimensions, so I would take both card_width
and card_height
out for the loops and make them constant values.