Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating items in Qt C++

Tags:

c++

qt

scene

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:

The scene

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:

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);
    }
}
like image 666
jvitasek Avatar asked Jan 18 '26 02:01

jvitasek


1 Answers

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:

  • x = 0
  • y = 0
  • line_brake = 0
  • width = 7
  • Considering card's width and height = 1

Start looping:

  • i = 0, j = 0 --> going in 3rd condition, card->setPos(0,0), next x=1 (card A)
  • i = 0, j = 1 --> going in 3rd condition, card->setPos(1,0), next x=2 (card B)
  • i = 0, j = 2 --> going in 3rd condition, card->setPos(2,0), next x=3 (card C)
  • i = 0, j = 3 --> going in 3rd condition, card->setPos(3,0), next x=4 (card D)
  • i = 0, j = 4 --> going in 3rd condition, card->setPos(4,0), next x=5 (card E)
  • i = 0, j = 5 --> going in 3rd condition, card->setPos(5,0), next x=6 (card F)
  • 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 = 1 --> going in 3rd condition, card->setPos(-1, 1), next x=0 (card I)
  • i = 1, j = 2 --> going in 3rd condition, card->setPos(0, 1), next x=1 (card J)
  • i = 1, j = 3 --> going in 3rd condition, card->setPos(1, 1), next x=2 (card K)
  • i = 1, j = 4 --> going in 3rd condition, card->setPos(2, 1), next x=3 (card L)
  • i = 1, j = 5 --> going in 3rd condition, card->setPos(3, 1), next x=4 (card M)
  • 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)

  • i = 2, j = 1 --> going in 3rd condition, card->setPos(-1, 2), next x=0 (card P)
  • i = 2, j = 2 --> going in 3rd condition, card->setPos(0, 2), next x=0 (card Q)
  • i = 2, j = 3 --> ...
  • i = 2, j = 4 --> ...

As the results:

enter image description here

like image 164
Martin Avatar answered Jan 19 '26 18:01

Martin



Donate For Us

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