qRubberBand not painted continuously in some cases

120 Views Asked by At

I have a problem with qRubberBand not painting continuous rectangles in some cases. I used exact example from Qt documentation:

void Widget::mousePressEvent(QMouseEvent *event)
{
    origin = event->pos();
    if (!rubberBand)
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    rubberBand->setGeometry(QRect(origin, QSize()));
    rubberBand->show();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    rubberBand->hide();
}

my problem is shown in this video:

https://imgur.com/a/TMpUz0l

as you can see, if I am painting the rectangle from left top to right bottom, the rectangle is painted smoothly. But otherwise the painting is not continuous.

Here are some events of my code:

void Graph::onMousePress(QMouseEvent* event){
    if ((event->buttons() & Qt::RightButton) == Qt::RightButton) {
        m_RMBPressed = true;
        m_globalOrigin = QPoint(event->globalPos().x(), event->globalPos().y());
        if (!m_rubberBand) {
            m_rubberBand = new QRubberBand(QRubberBand::Rectangle, qobject_cast<QWidget*>(this));
        }
        m_rubberBand->setGeometry(QRect(m_globalOrigin, QSize()));
        m_rubberBand->show();
    }
}

void Graph::onMouseRelease(QMouseEvent* event){
    m_RMBPressed = false;
    if (m_rubberBand) {
        m_rubberBand->hide();
    }
}

void Graph::onMouseMove(QMouseEvent* event){
    if (m_RMBPressed) {
        auto rectangle = QRect(QPoint(event->globalPos().x(), event->globalPos().y()), m_globalOrigin).normalized();
        m_rubberBand->setGeometry(rectangle);
        m_rubberBand->update();
    }
}

I tried to swap the coordinates of the QRect, remove the .normalized() function and then correct the rectangle coordinates but none of it worked. Any help is appreciated.

0

There are 0 best solutions below