Moving object using keyboard qt












-3















I'm trying to move an object using my arrow keys, but when I launch the app, nothing happens. Do you have an idea on how to fix it ?



@Update : my rectangle only moves once to the left and to the right, but if I use qDebug it recognizes all the times I click left or right, any ideas ?



void MouvementJoueur::keyPressEvent(QKeyEvent *e)
{
switch ( e->key() )
{
case Qt::Key_Left:
rectangle->setPos(x()-10,y());
qDebug() << "You pressed the Key left";
break;
case Qt::Key_Right:
rectangle->setPos(x()+10,y());
qDebug() << "You pressed the Key right";
break;
}
}


Thank you in advance !










share|improve this question




















  • 2





    "nothing happens" is rather vague error description. If that is indeed true, literally nothing seems to happen, problem is most likely in main function, such as never calling the event loop.

    – hyde
    Nov 21 '18 at 10:01











  • You should try to provide an MCVE if possible. Also, if you wish for others to read your code, it is highly recommended you indent it properly. It's just basic courtesy towards those who you wish to help you. All commonly used Qt IDEs (Qt Creator, Visual Studio, KDevelop...) have auto-indent or code formatting so there is no excuse to post poorly formatted code to Stack Overflow here.

    – hyde
    Nov 21 '18 at 10:03













  • @hyde I dit it but still I had to insert spaces in stackoverflow and idk why and what I mean is that if I click the right or left arrow, my rectangle does not move. What do you suggest?

    – David
    Nov 21 '18 at 10:08











  • To get the code formatted properly at SO, just paste it to the question edit textbox with your original correct indentation, and with empty lines below and above. Then select the entire code snippet, and click the code formatting icon {}, or keyboard shortcut ctrl-K.

    – hyde
    Nov 21 '18 at 13:47
















-3















I'm trying to move an object using my arrow keys, but when I launch the app, nothing happens. Do you have an idea on how to fix it ?



@Update : my rectangle only moves once to the left and to the right, but if I use qDebug it recognizes all the times I click left or right, any ideas ?



void MouvementJoueur::keyPressEvent(QKeyEvent *e)
{
switch ( e->key() )
{
case Qt::Key_Left:
rectangle->setPos(x()-10,y());
qDebug() << "You pressed the Key left";
break;
case Qt::Key_Right:
rectangle->setPos(x()+10,y());
qDebug() << "You pressed the Key right";
break;
}
}


Thank you in advance !










share|improve this question




















  • 2





    "nothing happens" is rather vague error description. If that is indeed true, literally nothing seems to happen, problem is most likely in main function, such as never calling the event loop.

    – hyde
    Nov 21 '18 at 10:01











  • You should try to provide an MCVE if possible. Also, if you wish for others to read your code, it is highly recommended you indent it properly. It's just basic courtesy towards those who you wish to help you. All commonly used Qt IDEs (Qt Creator, Visual Studio, KDevelop...) have auto-indent or code formatting so there is no excuse to post poorly formatted code to Stack Overflow here.

    – hyde
    Nov 21 '18 at 10:03













  • @hyde I dit it but still I had to insert spaces in stackoverflow and idk why and what I mean is that if I click the right or left arrow, my rectangle does not move. What do you suggest?

    – David
    Nov 21 '18 at 10:08











  • To get the code formatted properly at SO, just paste it to the question edit textbox with your original correct indentation, and with empty lines below and above. Then select the entire code snippet, and click the code formatting icon {}, or keyboard shortcut ctrl-K.

    – hyde
    Nov 21 '18 at 13:47














-3












-3








-3








I'm trying to move an object using my arrow keys, but when I launch the app, nothing happens. Do you have an idea on how to fix it ?



@Update : my rectangle only moves once to the left and to the right, but if I use qDebug it recognizes all the times I click left or right, any ideas ?



void MouvementJoueur::keyPressEvent(QKeyEvent *e)
{
switch ( e->key() )
{
case Qt::Key_Left:
rectangle->setPos(x()-10,y());
qDebug() << "You pressed the Key left";
break;
case Qt::Key_Right:
rectangle->setPos(x()+10,y());
qDebug() << "You pressed the Key right";
break;
}
}


Thank you in advance !










share|improve this question
















I'm trying to move an object using my arrow keys, but when I launch the app, nothing happens. Do you have an idea on how to fix it ?



@Update : my rectangle only moves once to the left and to the right, but if I use qDebug it recognizes all the times I click left or right, any ideas ?



void MouvementJoueur::keyPressEvent(QKeyEvent *e)
{
switch ( e->key() )
{
case Qt::Key_Left:
rectangle->setPos(x()-10,y());
qDebug() << "You pressed the Key left";
break;
case Qt::Key_Right:
rectangle->setPos(x()+10,y());
qDebug() << "You pressed the Key right";
break;
}
}


Thank you in advance !







c++ qt qgraphicsview qgraphicsscene






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:32







David

















asked Nov 21 '18 at 9:58









DavidDavid

618




618








  • 2





    "nothing happens" is rather vague error description. If that is indeed true, literally nothing seems to happen, problem is most likely in main function, such as never calling the event loop.

    – hyde
    Nov 21 '18 at 10:01











  • You should try to provide an MCVE if possible. Also, if you wish for others to read your code, it is highly recommended you indent it properly. It's just basic courtesy towards those who you wish to help you. All commonly used Qt IDEs (Qt Creator, Visual Studio, KDevelop...) have auto-indent or code formatting so there is no excuse to post poorly formatted code to Stack Overflow here.

    – hyde
    Nov 21 '18 at 10:03













  • @hyde I dit it but still I had to insert spaces in stackoverflow and idk why and what I mean is that if I click the right or left arrow, my rectangle does not move. What do you suggest?

    – David
    Nov 21 '18 at 10:08











  • To get the code formatted properly at SO, just paste it to the question edit textbox with your original correct indentation, and with empty lines below and above. Then select the entire code snippet, and click the code formatting icon {}, or keyboard shortcut ctrl-K.

    – hyde
    Nov 21 '18 at 13:47














  • 2





    "nothing happens" is rather vague error description. If that is indeed true, literally nothing seems to happen, problem is most likely in main function, such as never calling the event loop.

    – hyde
    Nov 21 '18 at 10:01











  • You should try to provide an MCVE if possible. Also, if you wish for others to read your code, it is highly recommended you indent it properly. It's just basic courtesy towards those who you wish to help you. All commonly used Qt IDEs (Qt Creator, Visual Studio, KDevelop...) have auto-indent or code formatting so there is no excuse to post poorly formatted code to Stack Overflow here.

    – hyde
    Nov 21 '18 at 10:03













  • @hyde I dit it but still I had to insert spaces in stackoverflow and idk why and what I mean is that if I click the right or left arrow, my rectangle does not move. What do you suggest?

    – David
    Nov 21 '18 at 10:08











  • To get the code formatted properly at SO, just paste it to the question edit textbox with your original correct indentation, and with empty lines below and above. Then select the entire code snippet, and click the code formatting icon {}, or keyboard shortcut ctrl-K.

    – hyde
    Nov 21 '18 at 13:47








2




2





"nothing happens" is rather vague error description. If that is indeed true, literally nothing seems to happen, problem is most likely in main function, such as never calling the event loop.

– hyde
Nov 21 '18 at 10:01





"nothing happens" is rather vague error description. If that is indeed true, literally nothing seems to happen, problem is most likely in main function, such as never calling the event loop.

– hyde
Nov 21 '18 at 10:01













You should try to provide an MCVE if possible. Also, if you wish for others to read your code, it is highly recommended you indent it properly. It's just basic courtesy towards those who you wish to help you. All commonly used Qt IDEs (Qt Creator, Visual Studio, KDevelop...) have auto-indent or code formatting so there is no excuse to post poorly formatted code to Stack Overflow here.

– hyde
Nov 21 '18 at 10:03







You should try to provide an MCVE if possible. Also, if you wish for others to read your code, it is highly recommended you indent it properly. It's just basic courtesy towards those who you wish to help you. All commonly used Qt IDEs (Qt Creator, Visual Studio, KDevelop...) have auto-indent or code formatting so there is no excuse to post poorly formatted code to Stack Overflow here.

– hyde
Nov 21 '18 at 10:03















@hyde I dit it but still I had to insert spaces in stackoverflow and idk why and what I mean is that if I click the right or left arrow, my rectangle does not move. What do you suggest?

– David
Nov 21 '18 at 10:08





@hyde I dit it but still I had to insert spaces in stackoverflow and idk why and what I mean is that if I click the right or left arrow, my rectangle does not move. What do you suggest?

– David
Nov 21 '18 at 10:08













To get the code formatted properly at SO, just paste it to the question edit textbox with your original correct indentation, and with empty lines below and above. Then select the entire code snippet, and click the code formatting icon {}, or keyboard shortcut ctrl-K.

– hyde
Nov 21 '18 at 13:47





To get the code formatted properly at SO, just paste it to the question edit textbox with your original correct indentation, and with empty lines below and above. Then select the entire code snippet, and click the code formatting icon {}, or keyboard shortcut ctrl-K.

– hyde
Nov 21 '18 at 13:47












2 Answers
2






active

oldest

votes


















1














Your issue seems to be, that you take position of the parent, and set position of rectangle based on that. This is probably not what you want to do with key presses here. You should set the position like this:



rectangle->setPos(rectangle->x() - 10, rectangle->y());





share|improve this answer































    1














    You can refresh the GUI with the following command



    view->processEvents();


    and you can debug it. It detect the key press or not.



    void MouvementJoueur::keyPressEvent(QKeyEvent *e)
    {
    if(e->key() == Qt::Key_Left)
    {
    rectangle->setPos(x()-10,y());
    qDebug() << "You pressed the Key x";
    }

    if(e->key() == Qt::Key_Right)
    {
    rectangle->setPos(x()+10,y());
    qDebug() << "You pressed the Key x";
    }
    }





    share|improve this answer
























    • Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..

      – David
      Nov 21 '18 at 10:19











    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',
    autoActivateHeartbeat: false,
    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%2f53409459%2fmoving-object-using-keyboard-qt%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Your issue seems to be, that you take position of the parent, and set position of rectangle based on that. This is probably not what you want to do with key presses here. You should set the position like this:



    rectangle->setPos(rectangle->x() - 10, rectangle->y());





    share|improve this answer




























      1














      Your issue seems to be, that you take position of the parent, and set position of rectangle based on that. This is probably not what you want to do with key presses here. You should set the position like this:



      rectangle->setPos(rectangle->x() - 10, rectangle->y());





      share|improve this answer


























        1












        1








        1







        Your issue seems to be, that you take position of the parent, and set position of rectangle based on that. This is probably not what you want to do with key presses here. You should set the position like this:



        rectangle->setPos(rectangle->x() - 10, rectangle->y());





        share|improve this answer













        Your issue seems to be, that you take position of the parent, and set position of rectangle based on that. This is probably not what you want to do with key presses here. You should set the position like this:



        rectangle->setPos(rectangle->x() - 10, rectangle->y());






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 13:43









        hydehyde

        31.8k1588132




        31.8k1588132

























            1














            You can refresh the GUI with the following command



            view->processEvents();


            and you can debug it. It detect the key press or not.



            void MouvementJoueur::keyPressEvent(QKeyEvent *e)
            {
            if(e->key() == Qt::Key_Left)
            {
            rectangle->setPos(x()-10,y());
            qDebug() << "You pressed the Key x";
            }

            if(e->key() == Qt::Key_Right)
            {
            rectangle->setPos(x()+10,y());
            qDebug() << "You pressed the Key x";
            }
            }





            share|improve this answer
























            • Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..

              – David
              Nov 21 '18 at 10:19
















            1














            You can refresh the GUI with the following command



            view->processEvents();


            and you can debug it. It detect the key press or not.



            void MouvementJoueur::keyPressEvent(QKeyEvent *e)
            {
            if(e->key() == Qt::Key_Left)
            {
            rectangle->setPos(x()-10,y());
            qDebug() << "You pressed the Key x";
            }

            if(e->key() == Qt::Key_Right)
            {
            rectangle->setPos(x()+10,y());
            qDebug() << "You pressed the Key x";
            }
            }





            share|improve this answer
























            • Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..

              – David
              Nov 21 '18 at 10:19














            1












            1








            1







            You can refresh the GUI with the following command



            view->processEvents();


            and you can debug it. It detect the key press or not.



            void MouvementJoueur::keyPressEvent(QKeyEvent *e)
            {
            if(e->key() == Qt::Key_Left)
            {
            rectangle->setPos(x()-10,y());
            qDebug() << "You pressed the Key x";
            }

            if(e->key() == Qt::Key_Right)
            {
            rectangle->setPos(x()+10,y());
            qDebug() << "You pressed the Key x";
            }
            }





            share|improve this answer













            You can refresh the GUI with the following command



            view->processEvents();


            and you can debug it. It detect the key press or not.



            void MouvementJoueur::keyPressEvent(QKeyEvent *e)
            {
            if(e->key() == Qt::Key_Left)
            {
            rectangle->setPos(x()-10,y());
            qDebug() << "You pressed the Key x";
            }

            if(e->key() == Qt::Key_Right)
            {
            rectangle->setPos(x()+10,y());
            qDebug() << "You pressed the Key x";
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 10:08









            erenbasturkerenbasturk

            33527




            33527













            • Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..

              – David
              Nov 21 '18 at 10:19



















            • Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..

              – David
              Nov 21 '18 at 10:19

















            Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..

            – David
            Nov 21 '18 at 10:19





            Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..

            – David
            Nov 21 '18 at 10:19


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409459%2fmoving-object-using-keyboard-qt%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?