Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    DrawRect and change color of pixels in all directions

    General and Desktop
    2
    2
    1211
    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.
    • M
      mythili last edited by

      Hi,
      I am going to change the color of pixels when its white by dragging the mouse from one point to another. I am drawing a rectangle over the startp point and endpoint i.e., drawRect and checking the each pixel color. In mouseMove I am able to draw the rect in all directions but I am not able to highlight the pixels in all directions. This is done only from TopLeft to BottomRight. I need this same in all directions. Please help me. Here is my code.

      @QPoint hili_start_point,hili_end_point;
      int hili_start_x,hili_start_y,hili_end_x,hili_end_y;
      QRect hiliselectionRect;

      void writingArea::paintEvent(QPaintEvent *event)
      {
      QPainter painter(this);
      painter.setPen(QPen(QBrush(QColor(0,0,0,180)),1,Qt::DashLine));
      painter.setBrush(QBrush(QColor(255,255,0,180)));
      painter.drawRect(hiliselectionRect);
      }

      void writingArea::mousePressEvent(QMouseEvent *event)
      {
      hili_start_point = event->pos();
      hili_start_x = hili_start_point.x();
      hili_start_y = hili_start_point.y();
      hiliselectionRect.setTopLeft(event->pos());
      hiliselectionRect.setBottomRight(event->pos());
      }

      void writingArea::mouseMoveEvent(QMouseEvent *event)
      {
      hiliselectionRect.setBottomRight(event->pos());
      update();
      }

      void writingArea::mouseReleaseEvent(QMouseEvent *event)
      {
      hili_end_point = event->pos();
      hili_end_x = hili_end_point.x();
      hili_end_y = hili_end_point.y();

          QRgb col;
          QRect draw_rect(hili_start_x,hili_start_y,hili_end_x - hili_start_x,hili_end_y-hili_start_y);
      
          QPixmap high_pixmap(draw_rect.size());
          QImage high_image = image.copy(draw_rect);
          QPainter high_painter(&image);
          int width = draw_rect.width();
          int height = draw_rect.height();
          for (int i = hili_start_x; i < width + hili_start_x; ++i)
          {
              for (int j = hili_start_y; j < height + hili_start_y; ++j)
              {
                  col = image.pixel(i, j);
                  QColor tempColor(col);
                  if (tempColor == Qt::white)
                  {
                      image.setPixel(i, j, qRgb(255,255,0));
                  }
              }
          }
          int rad = (myPenWidth / 2) + 2;
          update(draw_rect.normalized().adjusted(-rad, -rad, +rad, +rad));
      

      }@

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        [quote author="mythili" date="1369372022"]In mouseMove I am able to draw the rect in all directions but I am not able to highlight the pixels in all directions.[/quote]
        What exactly do you mean with "highlight the pixels in all directions"?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

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