PDA

View Full Version : Reference QGraphicsPixmapItem without adding it



jumico
6th April 2010, 03:52
What I want to happen is this. When a certain key is pressed.

QPixmap image ("path");
QGraphicsPixmapItem *pixmap = addPixmap(image);
pixmap->setRotation(x);

This works as it is but every time a key is pressed instead of rotating the current image it adds a new one rotated. So i need to be able to declare what pixmap is without adding it. I think I just need to change this line.

QGraphicsPixmapItem *pixmap = addPixmap(image);

And I could initialialy add it somewhere else.
Here is a portion of my current code.

void Scene::keyPressEvent( QKeyEvent *e )
{

QPixmap image ("path");
QGraphicsPixmapItem *pixmap = addPixmap(image);
switch ( e->key() )
{
case Qt::Key_Left:
{
pixmap->setRotation(x);
break;
}

Lykurg
6th April 2010, 08:28
Create the pixmap item in your constructor and store the pointer to it you a private member variable. Then you can easily access it.
class Scene : /*...*/
{
//...
private:
QGraphicsPixmapItem* m_pixmap;
}

Scene::Scene(/*...*/) : /*...*/
{
pixmap = addPixmap(QPixmap("path"));
}

void Scene::keyPressEvent( QKeyEvent *e )
{
switch ( e->key() )
{
case Qt::Key_Left:
{
m_pixmap->setRotation(x);
break;
}

jumico
6th April 2010, 17:50
Thanks a lot you're awesome. I knew it had to be something that simple but I just couldn't think of it.