Affichage Objet heritant de QObject et QGraphicsITem
bonjour,
j'ai fait un bout de code avec un pote qui affiche des objets 2D.
pour cela on a :
- une fenêtre contenant une scène.
elle récupère les images d'un objet pour les afficher.
- une classe abstraite GraphicsObject : public QObject, public QGraphicsItem.
qui prend en paramètre une QListe<Qpixmap*> et crée un tableau de QGraphicsPixmapItem
- des classes Magiciennes, Nuages... héritants de GraphicsObject
-un PixSender, qui stocke toutes les pixmap utiles à l'application dans des tableaux de QPixmap et les renvoie à un objet GraphicsObject lors de sa création.
voilà pour la petite histoire.
les sources ici : http://leszenfoires.fr/jdr
mais puisque mes objets graphiques héritent de QGraphicsItem, ne serait-il pas mieux d'ajouter directement un de ces objets à la scene ?
de faire scene->additem(magicienne); au lieu de scene->addPixmap(magicienne->getpixmap());
(magicienne étant un objet graphique)
ça ne renvoie pas d'erreur mais évidemment ça n'affiche rien.
je ne défini aucun parent dans le programme.
ça pourrait venir de là ?
ou peut être faire un truc du style magicienne->setPixmap(pixmapcourante)
(mais un QGraphicsItem n'a pas de membre setPixmap() )
enfin bref je ne trouve pas comment faire.
mon but final étant de pouvoir récupérer les events souris et clavier sur les objets.
une petite idée ?
GraphicsObject.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
/*
Classe Abstraite pour tous les objets graphiques
réimplémenter initpixList qui sert à afficher l'objet
*/
#ifndef HEADER_GRAPHICSOBJECT
#define HEADER_GRAPHICSOBJECT
#include <QGraphicsPixmapItem>
#include <iostream>
#include <QMouseEvent>
#include "PixSender.h"
#include <QPainter>
typedef struct M_Current{
M_Current(int a,QString s)
{
image=a;
state=s;
}
int image;
QString state;
}MCurrent; //cette structure permet de situer à quelle image et dans quelle position se trouve un personnage("stand", "walk" ...)
class GraphicsObject : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
GraphicsObject(QList<QPixmap*> pixObject,QPointF position = QPointF(300,200), MCurrent current = MCurrent(0,"stand")) : mCurrentImage(current){
setPos(position); //defini la position d e l'objet
mArea = pixObject.at(0)->size(); //récupère la taille de la premiere image pour boundingRect() et paint()
};
virtual ~GraphicsObject(){};
virtual void initPixList(QList<QPixmap*> pixObject,QPointF position = QPointF(300,200)) = 0;
void standAnimation(int max);
MCurrent getCurrentImage();
QGraphicsPixmapItem* getPixmap(int i, QString state);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
// virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
signals:
void OnObject();
protected :
QList <QGraphicsPixmapItem*> mStandPixmap; // tableau pour la position "stand"
QList <QGraphicsPixmapItem*> mWalkPixmap; // tableau pour la position "walk"
MCurrent mCurrentImage;
QSize mArea;
virtual void mousePressEvent(QGraphicsSceneHoverEvent * event);
};
#endif |
GraphicsObject.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include "GraphicsObject.h"
#include <iostream>
using namespace std;
void GraphicsObject :: standAnimation(int max)
{
mStandPixmap.at(mCurrentImage.image)->setVisible(0);
if (mCurrentImage.image==max-1) mCurrentImage.image = 0;
else mCurrentImage.image ++;
mStandPixmap.at(mCurrentImage.image)->setVisible(1);
}
QGraphicsPixmapItem* GraphicsObject :: getPixmap(int i, QString state)
{
if (state == "stand") return mStandPixmap.at(i);
return mWalkPixmap.at(i);
}
QRectF GraphicsObject :: boundingRect() const
{
return QRectF(pos().x(),pos().y(),mArea.width(),mArea.height());
}
void GraphicsObject :: paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
painter->drawRect(boundingRect());
} |