Moving Multiple Graphics Items with Keyboard Input.
-
Hi all,
Very new to QT, so I'm hoping this is an easy fix for someone more experienced.
I'm working on a Tetris clone, using the qt framework. I'm wondering what the best way is to move multiple graphics items with keyboard input. It seems like I can only set one graphics item as "focusable" at a time.
I can draw my tetris shape well, but as expected only one block in the shape with be able to move. Is there I way I can move them in unison on the screen?
Prior to moving...
And after moving....
I'm wondering if there is a clever way to just update the position of multiple QGraphicsItems (in my case, PixmapItems) with a single button press.
Here is my code so far:
#include <QApplication> #include <QGraphicsView> #include <QGraphicsRectItem> #include <QGraphicsScene> #include <QPixmap> #include <QDebug> #include "tertimino.h" const int M = 20; const int N = 10; const int pix_l = 18; int field[M][N]; struct Point {int x,y;} quad[4], quab[4]; int figures[7][4] = { 1,3,5,7, // I 2,4,5,7, // Z 3,5,4,6, // S 3,5,4,7, // T 2,3,5,7, // L 3,5,7,6, // J 2,3,4,5, // O }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QGraphicsScene * scene = new QGraphicsScene(); //clip tetris tile QRect recto(pix_l*3, 0, pix_l, pix_l); QPixmap image(":/tiles.png"); QPixmap copy ; copy = image.copy(recto); //set tetrimino shape int n = 0; for (int i = 0; i <4; i++) { quad[i].x= figures[n][i] % 2; quad[i].y= figures[n][i] / 2; } //int *dx = new int; for (int i=0; i <4; i++) { // create an item to add to the scene Tertimino * tet = new Tertimino(); tet->setPixmap(copy); tet->setPos(quad[i].x*pix_l,quad[i].y*pix_l); if (i==0){ tet->setFlag(QGraphicsItem::ItemIsFocusable); tet->setFocus(); } scene->addItem(tet); } QGraphicsView * view = new QGraphicsView(scene); view->show(); view->setFixedWidth(320); view->setFixedHeight(480); return a.exec(); }
Main.cpp
#include "tertimino.h" #include <QKeyEvent> #include <QPixmap> #include <QDebug> void Tertimino::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Left){ setPos(x()-18,y()); qDebug()<<this->x(); } else if (event->key() == Qt::Key_Right){ setPos(x()+18,y()); } else if (event->key() == Qt::Key_Up){ setPos(x(),y()-18); } else if (event->key() == Qt::Key_Down){ setPos(x(),y()+18);} }
Tertimino.cpp
#ifndef TERTIMINO_H #define TERTIMINO_H #include <QGraphicsPixmapItem> #include <QGraphicsRectItem> class Tertimino: public QGraphicsPixmapItem { public: void keyPressEvent(QKeyEvent* event); }; #endif // TERTIMINO_H
-
@ericarjun-t said in Moving Multiple Graphics Items with Keyboard Input.:
I'm wondering if there is a clever way to just update the position of multiple QGraphicsItems (in my case, PixmapItems) with a single button press.
Why not make every tetris item a block/class consisting out of multiple parts? As it is right now, you have just the parts forming the shape of your tetris item, but they are not connected and therefore only one part is moving.
Make a new class for your tetris items and put your block logic insidepaintEvent
.
To rotate the whole thing, you could use this instead of fiddling around with your own matrix.If you dont want to change your structure, you could maybe use
QGraphicsItemGroup
. -