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.










share|improve this question


















  • 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















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.










share|improve this question


















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 your QLabel instead?
    – vahancho
    Nov 8 at 11:13














  • 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








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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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
































    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 15:15









        user1396055

        604




        604






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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




















































































            Popular posts from this blog

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check