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