0

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.

nocturne
  • 627
  • 3
  • 12
  • 39
  • 1
    Please provide the version of the Qt and your target platform, compiler and other useful information. I could not reproduce the problem with Qt 5.15.1, Windows Minw32 8.1.0, Windows 10. – Soheil Armin Oct 05 '21 at 17:03
  • Hello, Qt version is 5.15.2, compiler MSVC 14.29.30133, target Windows 10. This strange behavior seems as if the selection window was painted only from left top to right bot, therefore if I am selecting in other direction the selection is still painted in that direction and result is not smooth. Also could it be problem that I have other paint event? Could it be "lagging" because of that? – nocturne Oct 06 '21 at 11:28
  • 1
    As I could not reproduce them problem and it's not likely that it's about the version and platform, I'm almost confident that the problem lies somewhere else. It would be a good idea to create a minimal project that have the same problem. share it on gist or github – Soheil Armin Oct 07 '21 at 09:52
  • Thank you for your effort. I think this is caused because of another paint event which could be responsible for the lagging. I did my own ruber band with just painting semi-transparent rectangle. Anyway, thank you for your time. If I could give you the bounty, I would. – nocturne Oct 07 '21 at 11:40
  • 1
    Glad to hear that. good luck – Soheil Armin Oct 07 '21 at 12:17

0 Answers0