Creating items in Qt C ++

I have some code to create map items for a Qt scene. This is what I came up with. The member functions being called are what you would get on behalf of, 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 what my scene looks like after running this code:

The scene

What could be the problem? I need the cards to be laid out in an area of ​​7 * 7. This means that there are 7 rows, 7 columns and in each field there is a map image.


This is what my scene looks like after editing @molbdnilo's suggestions:

The scene after edits

// 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);
    }
}

      

+3


source to share


2 answers


To be honest, I don't know why this code is causing problems. I also don't know why it is written in such a complex way. To create maps, 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);
    }
}

      



More. According to the picture, it looks like all cards are the same size, so I would choose for card_width

and card_height

for loops and make them constant values.

+3


source


Just to finish off the answer, vaancho gave you. When you run into such a problem, just take pen and paper and write your variable values ​​step by step. It's very simple and you can understand what the problem is.




Start with:

  • x = 0
  • y = 0
  • line_brake = 0
  • width = 7
  • Given the width and height of the card = 1



Cycle start:

  • i = 0, j = 0 -> in third state, card-> setPos (0,0), next x = 1 ( card A )
  • i = 0, j = 1 -> in third state, card-> setPos (1,0), next x = 2 ( card B )
  • i = 0, j = 2 -> in third state, card-> setPos (2,0), next x = 3 ( card C )
  • i = 0, j = 3 -> in third state, card-> setPos (3,0), next x = 4 ( card D )
  • i = 0, j = 4 -> in third state, card-> setPos (4,0), next x = 5 ( card E )
  • i = 0, j = 5 -> in third state, card-> setPos (5,0), next x = 6 ( card F )
  • i = 0, j = 6 -> in first condition, no setPos (), see default x, y in map constructor ?, added map probably at 0.0, line_break = 1 ( map G )

  • i = 1, j = 0 → goes to the second condition, y = 1, card-> setPos (6, 1), line_break = 0, next x = -1 ( card H )

  • i = 1, j = 1 -> in third state, card-> setPos (-1, 1), next x = 0 ( card I )
  • i = 1, j = 2 -> in third state, card-> setPos (0, 1), next x = 1 ( card J )
  • i = 1, j = 3 -> in third state, card-> setPos (1, 1), next x = 2 ( card K )
  • i = 1, j = 4 -> in third state, card-> setPos (2, 1), next x = 3 ( card L )
  • i = 1, j = 5 -> in third state, card-> setPos (3, 1), next x = 4 ( card M )
  • i = 1, j = 6 -> in first condition, no setPos (), see default x, y in map constructor ?, added map probably at 0.0, line_break = 1 (<strong map N)

  • i = 2, j = 0 → goes to the second condition, y = 2, card-> setPos (4, 2), line_break = 0, next x = -1 ( card O )

  • i = 2, j = 1 -> in third state, card-> setPos (-1, 2), next x = 0 ( card P )
  • i = 2, j = 2 -> in third state, card-> setPos (0, 2), next x = 0 ( card Q )
  • i = 2, j = 3 → ...
  • i = 2, j = 4 → ...

As results:

enter image description here

+5


source







All Articles