Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change pixels in an image.
QtWS25 Last Chance

Change pixels in an image.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    browser90
    wrote on last edited by
    #1

    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,

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • B Offline
        B Offline
        browser90
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Please mark your post as solved then.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved