Qt: How to Find the Position of the Mouseclick Relative to an Image
up vote
0
down vote
favorite
I would like to implement an image editor. I have a QPixmap in a QLabel in a QHBoxLayout. I have overriden the mousePressEvent in the parent widget. When the mousePressedEvent occurs, the
event->pos() = QPoint(271,115)
points to a place which is displaced relative to the pointer (mouse). The displacement is the distance of the QLabel from the QWidget's corner. It gets bigger when I resize the window. How do I find this displacement vector? I want to draw a pixel on the QPixmap exactly where the mouse is.
Note that the following methods give no remedy:
qDebug() << "event->pos()" << event->pos();
qDebug() << "this->pos() = " << this->pos();
qDebug() << "pm_imageLabel->pos() =" << pm_imageLabel->pos();
qDebug() << "pos = " << mapFromGlobal(QCursor::pos());
These give all different positions. No searching on the internet or in Qt's documentation brought me closer to the answer. Thank You in advance.
qt mouseevent mousepress
add a comment |
up vote
0
down vote
favorite
I would like to implement an image editor. I have a QPixmap in a QLabel in a QHBoxLayout. I have overriden the mousePressEvent in the parent widget. When the mousePressedEvent occurs, the
event->pos() = QPoint(271,115)
points to a place which is displaced relative to the pointer (mouse). The displacement is the distance of the QLabel from the QWidget's corner. It gets bigger when I resize the window. How do I find this displacement vector? I want to draw a pixel on the QPixmap exactly where the mouse is.
Note that the following methods give no remedy:
qDebug() << "event->pos()" << event->pos();
qDebug() << "this->pos() = " << this->pos();
qDebug() << "pm_imageLabel->pos() =" << pm_imageLabel->pos();
qDebug() << "pos = " << mapFromGlobal(QCursor::pos());
These give all different positions. No searching on the internet or in Qt's documentation brought me closer to the answer. Thank You in advance.
qt mouseevent mousepress
1
I think you have to subtract the label's position from the mouse event position to get coordinates relative to the label's top left corner, i.e.event->post() - pm_imageLabel->pos();
. However why don't you handle mouse events in yourQLabel
instead?
– vahancho
Nov 8 at 11:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to implement an image editor. I have a QPixmap in a QLabel in a QHBoxLayout. I have overriden the mousePressEvent in the parent widget. When the mousePressedEvent occurs, the
event->pos() = QPoint(271,115)
points to a place which is displaced relative to the pointer (mouse). The displacement is the distance of the QLabel from the QWidget's corner. It gets bigger when I resize the window. How do I find this displacement vector? I want to draw a pixel on the QPixmap exactly where the mouse is.
Note that the following methods give no remedy:
qDebug() << "event->pos()" << event->pos();
qDebug() << "this->pos() = " << this->pos();
qDebug() << "pm_imageLabel->pos() =" << pm_imageLabel->pos();
qDebug() << "pos = " << mapFromGlobal(QCursor::pos());
These give all different positions. No searching on the internet or in Qt's documentation brought me closer to the answer. Thank You in advance.
qt mouseevent mousepress
I would like to implement an image editor. I have a QPixmap in a QLabel in a QHBoxLayout. I have overriden the mousePressEvent in the parent widget. When the mousePressedEvent occurs, the
event->pos() = QPoint(271,115)
points to a place which is displaced relative to the pointer (mouse). The displacement is the distance of the QLabel from the QWidget's corner. It gets bigger when I resize the window. How do I find this displacement vector? I want to draw a pixel on the QPixmap exactly where the mouse is.
Note that the following methods give no remedy:
qDebug() << "event->pos()" << event->pos();
qDebug() << "this->pos() = " << this->pos();
qDebug() << "pm_imageLabel->pos() =" << pm_imageLabel->pos();
qDebug() << "pos = " << mapFromGlobal(QCursor::pos());
These give all different positions. No searching on the internet or in Qt's documentation brought me closer to the answer. Thank You in advance.
qt mouseevent mousepress
qt mouseevent mousepress
asked Nov 8 at 8:25
user1396055
604
604
1
I think you have to subtract the label's position from the mouse event position to get coordinates relative to the label's top left corner, i.e.event->post() - pm_imageLabel->pos();
. However why don't you handle mouse events in yourQLabel
instead?
– vahancho
Nov 8 at 11:13
add a comment |
1
I think you have to subtract the label's position from the mouse event position to get coordinates relative to the label's top left corner, i.e.event->post() - pm_imageLabel->pos();
. However why don't you handle mouse events in yourQLabel
instead?
– vahancho
Nov 8 at 11:13
1
1
I think you have to subtract the label's position from the mouse event position to get coordinates relative to the label's top left corner, i.e.
event->post() - pm_imageLabel->pos();
. However why don't you handle mouse events in your QLabel
instead?– vahancho
Nov 8 at 11:13
I think you have to subtract the label's position from the mouse event position to get coordinates relative to the label's top left corner, i.e.
event->post() - pm_imageLabel->pos();
. However why don't you handle mouse events in your QLabel
instead?– vahancho
Nov 8 at 11:13
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Finally I have figured it out partially with the help of vahancho. The the position of the QPixmap withing QLabel is difficult to determine, but I can forbid QLabel to resize. So I set the size of QLabel to the image size.
pm_imageLabel->setPixmap(m_pixmap);
pm_imageLabel->setFixedSize(m_pixmap.size());
and I override the mousePressed even inside QLabel class. This way the event->pos is correct.
Thanks.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Finally I have figured it out partially with the help of vahancho. The the position of the QPixmap withing QLabel is difficult to determine, but I can forbid QLabel to resize. So I set the size of QLabel to the image size.
pm_imageLabel->setPixmap(m_pixmap);
pm_imageLabel->setFixedSize(m_pixmap.size());
and I override the mousePressed even inside QLabel class. This way the event->pos is correct.
Thanks.
add a comment |
up vote
0
down vote
Finally I have figured it out partially with the help of vahancho. The the position of the QPixmap withing QLabel is difficult to determine, but I can forbid QLabel to resize. So I set the size of QLabel to the image size.
pm_imageLabel->setPixmap(m_pixmap);
pm_imageLabel->setFixedSize(m_pixmap.size());
and I override the mousePressed even inside QLabel class. This way the event->pos is correct.
Thanks.
add a comment |
up vote
0
down vote
up vote
0
down vote
Finally I have figured it out partially with the help of vahancho. The the position of the QPixmap withing QLabel is difficult to determine, but I can forbid QLabel to resize. So I set the size of QLabel to the image size.
pm_imageLabel->setPixmap(m_pixmap);
pm_imageLabel->setFixedSize(m_pixmap.size());
and I override the mousePressed even inside QLabel class. This way the event->pos is correct.
Thanks.
Finally I have figured it out partially with the help of vahancho. The the position of the QPixmap withing QLabel is difficult to determine, but I can forbid QLabel to resize. So I set the size of QLabel to the image size.
pm_imageLabel->setPixmap(m_pixmap);
pm_imageLabel->setFixedSize(m_pixmap.size());
and I override the mousePressed even inside QLabel class. This way the event->pos is correct.
Thanks.
answered Nov 8 at 15:15
user1396055
604
604
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203852%2fqt-how-to-find-the-position-of-the-mouseclick-relative-to-an-image%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
I think you have to subtract the label's position from the mouse event position to get coordinates relative to the label's top left corner, i.e.
event->post() - pm_imageLabel->pos();
. However why don't you handle mouse events in yourQLabel
instead?– vahancho
Nov 8 at 11:13