WASD movement doesn't wok



  • Hi all,
    I'm doing a program where you can move a green circle using wasd, but unfortunately when I press those keys it doesn't happen anything. I don't understand if it doesn't read my keyboard or if is not repainting the window.
    Please don't kill me, I've already checked the solved post and I couldn't find the answer. I'm a very newbye.

    Here's the code:

    circle.cpp
    @#include "circle.h"

    Circle::Circle(QWidget *parent) :
    QWidget(parent)
    {
    //settaggio delle coordinate(?centro del cerchio?) e raggio del cerchio
    x=width()/2;
    y=height()/2;
    r=100;
    //non ne ho idea
    antialiased = false;
    //settaggio delle dimensioni della finestra a "espansibili"
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    }

    void Circle::SetAntialiased(bool antialiased){
    this->antialiased = antialiased;
    update();
    }

    void Circle::NextAnimation(QKeyEvent event){
    switch(event->key()){
    case Qt::Key_A:
    x -= 10;
    break;
    case Qt::Key_D:
    x += 10;
    break;
    case Qt::Key_S:
    y -= 10;
    break;
    case Qt::Key_W:
    y += 10;
    break;
    case Qt::Key_P:
    r += (r
    5/100);
    break;
    case Qt::Key_L:
    r -= (r*5/100);
    break;
    default:
    break;
    }
    update();
    }

    void Circle::paintEvent(QPaintEvent *){
    QPainter painter(this);
    //?????????????????
    painter.setRenderHint(QPainter::Antialiasing, antialiased);
    //mette il cerchio al centro della finestra
    painter.translate(width()/2, height()/2);
    painter.setPen(QPen(Qt::green));
    painter.setBrush(QBrush(Qt::green));
    painter.drawEllipse(x,y,r,r);
    }
    @

    window.cpp
    @#include "window.h"

    Window::Window(){
    QTimer *timer = new QTimer(this);
    puntino = new Circle;
    connect(timer, SIGNAL(timeout()), puntino, SLOT(NextAnimation()));
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(puntino);
    timer->start();
    setLayout(layout);
    setWindowTitle(tr("Muovi il puntino"));
    }
    @

    main
    @#include <QApplication>
    #include <QtGui>
    #include "window.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Window window;
    window.showMaximized();
    return a.exec();
    }@

    Please help me.



  • Oh, maybe circle.h can help you

    @class Circle : public QWidget
    {
    Q_OBJECT
    public:
    Circle(QWidget parent = 0);
    void SetAntialiased(bool antialiased);
    void keyPressItEvent(QKeyEvent
    event, size_t& x, size_t& y);
    signals:

    public slots:
    void NextAnimation(QKeyEvent *event);
    protected:
    void paintEvent(QPaintEvent *event);
    private:
    int x;
    int y;
    int r;
    bool antialiased;
    };@



  • seems like you are connecting your timer with the Circle::NextAnimation method, but the timeout signal has no QKeyEvent parameter, so that connections doesn't match and you should get an runtime warning for the connect call?

    If you want to do event based animation (only redraw when you press a key), you don't need a timer, just override this method in your widget:
    @
    QWidget::keyPressEvent(QKeyEvent * event)
    // or void QWidget::keyReleaseEvent(QKeyEvent * event) whatever you prefer
    @
    then you should have a valid QKeyEvent and after you have changed the positions you can manually redraw the widget with
    @
    QWidget::update()
    @
    which updates the window and repaints it, your can also directly call QWidget::repaint() but read the doc, update is better in most cases.

    Edit: QWidget::update() will call your paintEvent method, you so you know what happens.



  • If I put QWidget::keyPressEvent(QKeyEvent * event) instead of NextAnimation() it doesn't recognize the variables x,y,r... Am I applying it wrong?



  • No you don't have to replace it, you need to implement that keyPressEvent method in your main widget class (which you didn't post here I think?), I guess your Window if that is your root QWidget.
    you could also implement it in your Circle class but then every circle has its own key event and only one widget can have key focus at a single time, so it's better to have one key event in the main window and, I don't know how your app works if you have more than 1 circle, you need to select one and then move it with the keys?

    anyway a little help maybe, you need to do it in the same way as the paintEvent:
    @
    protected:
    void paintEvent(QPaintEvent *event);
    void keyPressEvent(QKeyEvent * event); // and then implement it in your cpp file
    @
    but as I said it depends in which widget you want to implement it, I would say in the main widget or the circle if you can only have one active circle to move around.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.