goodle06
goodle06

Reputation: 11

QPainter paints only part of the rectangle over QImage

I'm loading image into QImage from data in memory, I've managed successfully paint it with QPainter onto QWidget. Then I'm trying to change some pixels of this QImage to highlight rectangle area using rubber band. My problem is that I'm getting only part of the rectangle, and sometimes nothing at all (see picture below).Only one line when it should be four Code .h:

class PaintWidget : public QWidget
{
    Q_OBJECT
public:
    using QWidget::QWidget;

    void setSourceSample(Sample src) ;
    void drawRectangle(OCRRectangle rect);
protected:
    void paintEvent(QPaintEvent *event) override;
    void mousePressEvent(QMouseEvent *ev) override;
    void mouseMoveEvent(QMouseEvent *ev) override;
    void mouseReleaseEvent(QMouseEvent *ev) override;

private:
    QRectF target=QRectF(0, 0, 1000, 1000);
    QImage p_source;
    QPoint origin, end;
    QRubberBand * rubberBand;
    QPair<float, float> rsz_ratio;
    void calcROI();
    QPoint translateCoordinates(cv::Point point);
};

Code .cpp:

void PaintWidget::paintEvent(QPaintEvent *event) {
    QPainter p(this);
    p.drawImage(target, p_source);
}
void PaintWidget::mousePressEvent(QMouseEvent *ev) {
    origin=ev->pos();
    rubberBand=new QRubberBand(QRubberBand::Rectangle, this);
    rubberBand->setGeometry(QRect(origin, QSize()));
    rubberBand->show();
}
void PaintWidget::mouseMoveEvent(QMouseEvent *ev) {
    rubberBand->setGeometry(QRect(origin, ev->pos()).normalized());
}
void PaintWidget::mouseReleaseEvent(QMouseEvent *ev) {
    end=ev->pos();
    rubberBand->hide();
    delete rubberBand;
    calcROI();
}
void PaintWidget::calcROI() {
    int p1x=min(end.x(),origin.x())*rsz_ratio.first;
    int p2x=max(end.x(),origin.x())*rsz_ratio.first;
    int p1y=min(end.y(), origin.y())*rsz_ratio.second;
    int p2y=max(end.y(), origin.y())*rsz_ratio.second;
    drawRectangle(OCRRectangle(cv::Point(p1x, p1y),cv::Point(p2x, p2y)));
}
void PaintWidget::setSourceSample(Sample src) {
    QImage inter=src.toQImage();
    p_source=inter.convertToFormat(QImage::Format::Format_RGB32);
    rsz_ratio.first=(float)src.size().width/(float)(target.width());
    rsz_ratio.second=(float)src.size().height/(float)target.height();
}
QPoint PaintWidget::translateCoordinates(cv::Point point) {
    QPoint qtpoint;
    qtpoint.setX(point.x);
    qtpoint.setY(point.y);
    return qtpoint;
}
void PaintWidget::drawRectangle(OCRRectangle rect) {
    QPoint p1(translateCoordinates(rect.p1)), p2(translateCoordinates(rect.p2));
    for (int i=p1.x(); i<=p2.x(); i++) {
       p_source.setPixelColor(i,p1.y(),QColor(255,0,0));
       p_source.setPixelColor(i, p2.y(), QColor(255,0,0));
    }
    for (int i=p1.y(); i<=p2.y(); i++) {
       p_source.setPixelColor(p1.x(),i,QColor(255,0,0));
       p_source.setPixelColor(p2.x(),i,QColor(255,0,0));
    }
}

UPDATE: it seems that the problem lies within rescaling of original image. When I'm drawing filled red rectangle it goes absolutely fine, my guess is that rescaling 1px border rectangle is just erasing borders. But setting thicker borders (up to 5 pixels) doesn't provide me with solution.

Upvotes: 0

Views: 274

Answers (1)

goodle06
goodle06

Reputation: 11

Making thicker borders solved this problem.

Upvotes: 0

Related Questions