Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Change pixels in an image.

    General and Desktop
    2
    4
    1604
    Loading More Posts
    • 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
      browser90 last edited by

      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 Reply Quote 0
      • K
        koahnig last edited by

        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 Reply Quote 0
        • B
          browser90 last edited by

          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 Reply Quote 0
          • K
            koahnig last edited by

            Please mark your post as solved then.

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post