Results 1 to 2 of 2

Thread: DrawRect and change color of pixels in all directions

  1. #1
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default DrawRect and change color of pixels in all directions

    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:aintEvent(QPaintEvent *event)
    {
    QPainter painter(this);
    painter.setPen(QPen(QBrush(QColor(0,0,0,180)),1,Qt :ashLine));
    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));
    }

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: DrawRect and change color of pixels in all directions

    This should help
    Qt Code:
    1. void writingArea::mouseReleaseEvent(QMouseEvent *event)
    2. {
    3. ...
    4. int x1 = qMin(hili_start_x, hili_end_x);
    5. int x2 = qMax(hili_start_x, hili_end_x);
    6.  
    7. int y1 = qMin(hili_start_y, hili_end_y);
    8. int y2 = qMax(hili_start_y, hili_end_y);
    9.  
    10. hili_start_x = x1;
    11. hili_end_x = x2;
    12.  
    13. hili_start_y = y1;
    14. hili_end_y = y2;
    15.  
    16. QRect draw_rect(hili_start_x,hili_start_y,hili_end_x - hili_start_x,hili_end_y-hili_start_y);
    17. ...
    18. }
    To copy to clipboard, switch view to plain text mode 

    or one other way
    Qt Code:
    1. void writingArea::mouseReleaseEvent(QMouseEvent *event)
    2. {
    3. ...
    4. QRect draw_rect(hili_start_x,hili_start_y,hili_end_x - hili_start_x,hili_end_y-hili_start_y);
    5.  
    6. draw_rect = draw_rect.normalized();
    7.  
    8. hili_start_x = draw_rect.topLeft().x();
    9. hili_end_x = draw_rect.bottomRight().x();
    10.  
    11. hili_start_y = draw_rect.topLeft().y();
    12. hili_end_y = draw_rect.bottomRight().y();
    13. ...
    14. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    mythili (24th May 2013)

Similar Threads

  1. Replies: 1
    Last Post: 15th August 2012, 18:00
  2. Change in color map?
    By bigjoeystud in forum Qwt
    Replies: 4
    Last Post: 8th September 2010, 20:00
  3. Replies: 3
    Last Post: 22nd January 2010, 16:46
  4. How to change the values of the pixels in an Qimage?
    By kid17 in forum Qt Programming
    Replies: 8
    Last Post: 23rd November 2008, 20:52
  5. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.