Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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::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));
    

    }@


  • Moderators

    [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"?


Log in to reply