What is the common pattern to implement a selected region dragged by mouse ?



  • I am quite new on Qt, and I would like to implement a selection made by dragging with the mouse.

    I have found a clear and nice example which used QRubberBand class that I think will be a good inspiration for what i want to implement:
    @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();
    // determine selection, for example using QRect::intersects()
    // and QRect::contains().
    }@

    It will be cool if we can draw from any direction top-left/right corner and bottom-left/right corner, is the example include those things or otherwise what is the "logic" to implement a selection that can start dragging with any direction?

    Thks in advance for any ideas or enligthenment about those things :)


  • Moderators

    since you use QRect::normalized() it should work as expected.

    But why don't you just try it yourself within 2 mins?!



  • Ok, thanks, i just wanted to know if at looking at the code, it is possible to see where "the any direction thing" is implemented as i am trying to understand the structure of this kind of code(mouse event...)
    Sorry, the compilation is very long on my computer(don't know if it is because of my computer or the program..), but i will try when i will have long free time :)


Log in to reply
 

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