Can't see rubberband



  • Hello.

    I've successfully managed to get working rectangle-zooming in a QLabel, more of less the way I want.

    I've done this via eventFilter() in my main class, and installing the usual event filter for the QLabel so that the main class will be managing this. It makes sense since the images are used in many places and needed to be global anyway.

    Now I am trying to make this more visually appealing, by drawing a rubber band over the selection, so the user knows what to expect when s/he releases the mouse button.

    The examples I've found work via mousePressEvent(), mouseMoveEvent() and mouseReleaseEvent(). I am trying to adapt that to work in the eventFilter(), since it's there that I am doing all the work. So I ended up with this:

    @
    bool kooker::eventFilter(QObject *object, QEvent *event)
    {
    if(object == ui->gv_foto_medio)
    {
    if(event->type() == QEvent::MouseButtonPress)
    {
    QMouseEvent me = static_cast<QMouseEvent>(event);
    pos_global = me->pos();
    return true;
    }
    else if(event->type() == QEvent::MouseButtonRelease)
    {
    QMouseEvent me = static_cast<QMouseEvent>(event);
    QPoint pos = me->pos();
    if(pos == pos_global)
    {
    re_zoom_foto_media();
    return true;
    }
    qDebug() << Q_FUNC_INFO << QString("Mouse was pressed at %1,%2 and released at %3,%4").arg(pos_global.x()).arg(pos_global.y()).arg(pos.x()).arg(pos.y());
    double posx, posy;
    if(pos_global.x() < pos.x())
    {
    posx = pos_global.x();
    }
    else
    {
    posx = pos.x();
    }

            if(pos_global.y() < pos.y())
            {
                posy = pos_global.y();
            }
            else
            {
                posy = pos.y();
            }
    
            raton_sobre_foto_mediana(posx, posy);
            return true;
        }
    }
    else if(object == ui->gv_plano_medio)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *me = static_cast<QMouseEvent*>(event);
            pos_global = me->pos();
    
            if (rubberBand == NULL)
            {
                rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
            }
            rubberBand->setGeometry(QRect(pos_global, QSize()));
            rubberBand->show();
    
            return true;
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *me = static_cast<QMouseEvent*>(event);
            QPoint pos = me->pos();
            if(pos == pos_global)
            {
                re_zoom_plano_medio();
                return true;
            }
            qDebug() << Q_FUNC_INFO << QString("Mouse was pressed at %1,%2 and released at %3,%4").arg(pos_global.x()).arg(pos_global.y()).arg(pos.x()).arg(pos.y());
            double posx, posy;
            if(pos_global.x() < pos.x())
            {
                posx = pos_global.x();
            }
            else
            {
                posx = pos.x();
            }
    
            if(pos_global.y() < pos.y())
            {
                posy = pos_global.y();
            }
            else
            {
                posy = pos.y();
            }
    
            raton_sobre_plano_mediano(posx, posy);
            rubberBand->hide();
            return true;
        }
    }
    else if(event->type() == QEvent::MouseMove)
    {
        QMouseEvent *me = static_cast<QMouseEvent*>(event);
        QPoint pos = me->pos();
        rubberBand->setGeometry(QRect(pos_global, pos).normalized());
        rubberBand->show();
        return true;
    }
    
    return QObject::eventFilter(object, event);
    

    }
    @

    As said, the zooming works ok. However, I see no rubberband at all. I've tried to set the rectangle to something like (10,10,80,80) in the mouseMoveEvent part, just to make sure there wasn't any funkiness going on with the coordinates, but no difference...

    I also tried to do as in the examples, and use those methods I mentioned above in my QLabel derivative. But it didn't work either (I really didn't expect it to, since it's the main class who's filtering the events anyway).

    I've been some days tinkering with this, but I haven't ever seen a rubberband, outside my KDE install :P

    Thanks for reading and for any tip you can share :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.