Moving object using keyboard qt
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
add a comment |
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
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
add a comment |
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
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
c++ qt qgraphicsview qgraphicsscene
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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());
add a comment |
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";
}
}
Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..
– David
Nov 21 '18 at 10:19
add a comment |
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
});
}
});
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
Required, but never shown
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
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());
add a comment |
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());
add a comment |
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());
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());
answered Nov 21 '18 at 13:43
hydehyde
31.8k1588132
31.8k1588132
add a comment |
add a comment |
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";
}
}
Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..
– David
Nov 21 '18 at 10:19
add a comment |
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";
}
}
Thank you, it recognizes but my rectangle isnt shown on the screen, its invisible..
– David
Nov 21 '18 at 10:19
add a comment |
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";
}
}
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";
}
}
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
add a comment |
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
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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