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);
}
}
Just to complete the answer vahancho gave you. When you encounter a problem like this, just take a pen and a paper and write step by step your variables values. It's very simple and you can figure out what the problem is.
Start with:
Start looping:
i = 0, j = 6 --> going in 1st condition, no setPos(), see default x,y value in card constructor ?, card added probably in 0,0, line_break=1 (card G)
i = 1, j = 0 --> going in 2nd condition, y=1, card->setPos(6, 1), line_break=0, next x=-1 (card H)
i = 1, j = 6 --> going in 1st condition, no setPos(), see default x,y value in card constructor ?, card added probably in 0,0, line_break=1 (card N)
i = 2, j = 0 --> going in 2nd condition, y=2, card->setPos(4, 2), line_break=0, next x=-1 (card O)
As the results:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With