Change pixels in an image.



  • Hi guys,

    i'm having a QImage worldImage in my class as private var, when i use my damage function
    it draws a black circle on the worldImage that works fine, here is the code for that part:

    @//function to add damage to the land
    void World::damage(int x, int y)
    {
    QPainter painter(&worldImage);
    painter.setBrush(QBrush(Qt::black));
    painter.drawEllipse(x,y,150,100);
    item = new QGraphicsPixmapItem(QPixmap::fromImage(worldImage));
    this->addItem(item);
    putWorm(this->x, this->y);
    }

    then after the black circle is on i need to update my world and i would use this function

    void World::updateWorld()
    {

    for(int k = 0; k < height; k++)
    {
        for(int l = 0; l < width; l++)
        {
            color = worldImage.pixel(k,l);
            if (qRed(color) == 0 && qGreen(color) == 0 && qBlue(color) == 0) {
                worldImage.setPixel(k,l,QColor(Qt::blue).rgb());
                bitmap[k][l] = 0;
            }
            else
                bitmap[k][l] = 1;
        }
    }
    

    }@

    this makes my bitmap for collision detection up to date and where the black circle is, it's going to be blue.
    But the code doesn't work, it sais it is out of range with every pixel , and if i ask the height and width of the image
    it is still the same image. Is it not possible to change the image this way do i need to create a new one or ...?

    Kind regards,


  • Moderators

    You need to look in the debugger for the values in height, width and also bitmap, if this is matching. I do not see, how one can help you with the code you have posted. The key elements are missing.
    Where did you define these values in question?



  • Solved, I thought worldImage was my new image but it is just the painter, the original background.png is changed. I changed the two and everything works like a charm.


  • Moderators

    Please mark your post as solved then.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.