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. DrawRect and change color of pixels in all directions

DrawRect and change color of pixels in all directions

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.3k Views 1 Watching
  • 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 Offline
    M Offline
    mythili
    wrote on last edited by
    #1

    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
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      [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
      0

      • Login

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