Qt fangen gedrückten Taste

denke ich an zu schreiben, primitiven Schlange. ich habe Fenster, wo das Programm die Farben zufällige Linien.

aber ich kann mir nicht denken, wie zu fangen gedrückte Taste zum ändern der Richtung der gemalten Linie.

Qt fangen gedrückten Taste

class QUpdatingPathIte : public QGraphicsPathItem
{
    void advance(int phase)
    {

        if (phase == 0)
            return;

        int x,y;
        int w,a,s,d;

        char c;
        //
        //HOW TO    MAKE THIS HERE? (i had done early in console application)
        //but i can't do this in GUI , what should i  do?

        scanf("%c",&c);
        if (c=='w')
        { x=-20; y=0; }
        else if (c=='s')
        { x=20; y=0; }
        else if (c=='a')
        { x=0; y=-20; }
        else if(c=='d')
        { x=0; y=20; }

        QPainterPath p = path();
        p.lineTo(x, y);
        setPath(p);
    }
};

#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene s;
    QGraphicsView v(&s);

    QUpdatingPathIte item;
    item.setPen(QPen(QColor("black")));
    s.addItem(&item);
    v.show();

    QTimer *timer = new QTimer(&s);
    timer->connect(timer, SIGNAL(timeout()), &s, SLOT(advance()));
    timer->start(500);

    return a.exec();
}

InformationsquelleAutor Tebe | 2011-05-21

Schreibe einen Kommentar