Get QPainter pos
-
My image is rendered inside a widget if it is larger than the widget can be moved, so I need to know what is the current position of the image.
@
_result_image = QImage();
_result_image.load("D:\Desert.jpg");
@@
void OpenCVWidget::paintEvent(QPaintEvent *)
{QPainter painter(this); painter.drawImage(QPoint(0, 0), _result_image); painter.end();
}
void OpenCVWidget::mousePressEvent(QMouseEvent *event)
{event->accept();
}
void OpenCVWidget::mouseMoveEvent(QMouseEvent *event)
{event->accept();
}
void OpenCVWidget::mouseReleaseEvent(QMouseEvent *event)
{event->accept();
}
@ -
Try Qt Quick, it's really what you need. Use "Flickable":http://qt-project.org/doc/qt-4.8/qml-flickable.html
@
import QtQuick 1.1
Flickable {
width: 200
height: 200
contentHeight: image.height
contentWidth: image.width
Image {
id: image
source: "http://www.symbiantweet.com/wp-content/uploads/2011/05/qt-logo.jpg"
}
}
@You can change source to "D:\Desert.jpg"
-
[quote author="beemaster" date="1351362895"]Try Qt Quick, it's really what you need.[/quote]I doubt it...
To the question:
- have a member variable save the current camera position (or image position - ends up being the same thing). Let's call it QPoint mImagePos.
- In mouse press event, save the current cursor position to QPoint mDragStartCursorPos and save the current image position mImagePos to QPoint mDragStartImagePos
- In mouse move event, calculate the distance vector between mDragStartCursorPos and the current cursor position. Then set the current image position mImagePos to mDragStartImagePos plus the distance vector. And, of course, call repaint() to cause a paint event.
- On the mouse release event, finally set the image position mImagePos to mDragStartImagePos plus the distance vector.
- In the paint event, draw the QImage at mImagePos, always.
Note that you could also implement this via the QPainter::translate functionality. This will allow dragging of a whole scene painted by the painter, without needing dynamic coordinates for the objects (like we used mImagePos here). This depends on what you really want: Single object dragging, or scene dragging.