Results 1 to 7 of 7

Thread: How to get aspect ratio on selection with rubberBand?

  1. #1
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to get aspect ratio on selection with rubberBand?

    I want to use aspect ratio (for example, 1:3) when make a selection with rubberBand. How can i do it? By default it make selection with 1:1 ratio... in function mouseMoveEvent.

    mywidget.h:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyWidget : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. MyWidget (QWidget* parent = 0);
    8. protected:
    9. void mouseMoveEvent(QMouseEvent *event);
    10. void mousePressEvent(QMouseEvent *event);
    11. void mouseReleaseEvent(QMouseEvent *event);
    12. private:
    13. QLabel* m_pixmapLabel;
    14. QRubberBand* rubberBand;
    15. QPoint origin;
    16. };
    To copy to clipboard, switch view to plain text mode 


    main.cpp:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include "mywidget.h"
    4.  
    5. MyWidget::MyWidget(QWidget* parent): QWidget(parent)
    6. {
    7. m_pixmapLabel = new QLabel;
    8. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    9. QVBoxLayout* mainLayout = new QVBoxLayout;
    10. QPixmap pm;
    11. pm.load("image.jpg");
    12. m_pixmapLabel->setPixmap(pm);
    13. mainLayout->addWidget(m_pixmapLabel);
    14. setLayout(mainLayout);
    15.  
    16. }
    17.  
    18. void MyWidget::mouseMoveEvent(QMouseEvent *event)
    19. {
    20. rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
    21.  
    22. }
    23.  
    24. void MyWidget::mousePressEvent(QMouseEvent *event)
    25. {
    26. origin = event->pos();
    27. rubberBand->setGeometry(QRect(origin, QSize()));
    28. rubberBand->show();
    29. }
    30.  
    31. void MyWidget::mouseReleaseEvent(QMouseEvent *event)
    32. {
    33. rubberBand->hide();
    34.  
    35. }
    36.  
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QApplication application(argc, argv);
    41. MyWidget window;
    42. window.show();
    43. return application.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get aspect ratio on selection with rubberBand?

    Call setGeometry() on the rubber band with the size recalculated to take the aspect ratio into consideration.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get aspect ratio on selection with rubberBand?

    i've tried inside mouseMoveEvent function:
    Qt Code:
    1. rubberBand->setGeometry(QRect(origin, QPoint(event->y() * 1.3, event->y())).normalized());
    To copy to clipboard, switch view to plain text mode 

    but selection works strange...
    1. when i start dragging the mouse to bottom selection grows, but when i start dragging mouse to the right - no, how can i fix it?
    2. default width of selection is more longer if i begin selection nearer bottom.. how can i fix it?
    3.selection doesn't correctly work if i do it near the upper - right area

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to get aspect ratio on selection with rubberBand?

    For starters, using event->y() is not going to work, and is likely the cause of all your problems. This gives you the position of the mouse relative to the widget the event takes place in, which isn't what you want. You want the position relative to the point where the mouse button went down, and you don't just want the position - you want the difference between the position and the mouse down point. Note how the Qt example documentation uses both mousePress and mouseMove events to keep track of the origin.

    The approach your code takes is also problematic when the mouse gets dragged near the left or right edges, and your calculation comes up with a width that's outside the widget area.

  5. #5
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get aspect ratio on selection with rubberBand?

    Quote Originally Posted by SixDegrees View Post
    For starters, using event->y() is not going to work, and is likely the cause of all your problems. This gives you the position of the mouse relative to the widget the event takes place in, which isn't what you want. You want the position relative to the point where the mouse button went down, and you don't just want the position - you want the difference between the position and the mouse down point. Note how the Qt example documentation uses both mousePress and mouseMove events to keep track of the origin.

    The approach your code takes is also problematic when the mouse gets dragged near the left or right edges, and your calculation comes up with a width that's outside the widget area.
    You said, that i need difference between the position and the mouse down point, then which variables are used for "position" and "mouse down point"?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get aspect ratio on selection with rubberBand?

    In mousePressEvent() you have to store the mouse down point in some variable and then use it to calculate the difference between the position in mouse move and the one stored from mouse press. If you don't know what we mean then take a piece of paper, draw your widget with the rubberband on it and try to come up with an algorithm to calculate the coordinates of the rectangle.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get aspect ratio on selection with rubberBand?

    big thanks for all, i finally did it.

Similar Threads

  1. rubberband selection
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 22nd March 2010, 18:55
  2. Rubberband Item Selection
    By zgulser in forum Qt Programming
    Replies: 5
    Last Post: 23rd October 2009, 08:48
  3. Keep aspect size policy?
    By brcain in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2009, 17:18
  4. Replies: 3
    Last Post: 14th March 2007, 08:09
  5. QGraphicsView rubberband selection
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2007, 21:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.