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>
{
Q_OBJECT
public:
protected:
private:
};
#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;
};
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QApplication>
#include <QtGui>
#include "mywidget.h"
{
pm.load("image.jpg");
m_pixmapLabel->setPixmap(pm);
mainLayout->addWidget(m_pixmapLabel);
setLayout(mainLayout);
}
{
rubberBand
->setGeometry
(QRect(origin, event
->pos
()).
normalized());
}
{
origin = event->pos();
rubberBand->show();
}
{
rubberBand->hide();
}
int main(int argc, char *argv[])
{
MyWidget window;
window.show();
return application.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks