PDA

View Full Version : How to get aspect ratio on selection with rubberBand?



suseway
24th October 2010, 10:19
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:



#include <QtGui>

class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget (QWidget* parent = 0);
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
QLabel* m_pixmapLabel;
QRubberBand* rubberBand;
QPoint origin;
};




main.cpp:



#include <QApplication>
#include <QtGui>
#include "mywidget.h"

MyWidget::MyWidget(QWidget* parent): QWidget(parent)
{
m_pixmapLabel = new QLabel;
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
QVBoxLayout* mainLayout = new QVBoxLayout;
QPixmap pm;
pm.load("image.jpg");
m_pixmapLabel->setPixmap(pm);
mainLayout->addWidget(m_pixmapLabel);
setLayout(mainLayout);

}

void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
rubberBand->setGeometry(QRect(origin, event->pos()).normalized());

}

void MyWidget::mousePressEvent(QMouseEvent *event)
{
origin = event->pos();
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}

void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
rubberBand->hide();

}


int main(int argc, char *argv[])
{
QApplication application(argc, argv);
MyWidget window;
window.show();
return application.exec();
}

wysota
24th October 2010, 10:39
Call setGeometry() on the rubber band with the size recalculated to take the aspect ratio into consideration.

suseway
24th October 2010, 10:58
i've tried inside mouseMoveEvent function:


rubberBand->setGeometry(QRect(origin, QPoint(event->y() * 1.3, event->y())).normalized());


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

SixDegrees
24th October 2010, 12:58
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.

suseway
24th October 2010, 21:37
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"?

wysota
24th October 2010, 21:43
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.

suseway
25th October 2010, 08:29
big thanks for all, i finally did it.