Athena
Athena

Reputation: 543

two bugs in adding items to a QGraphicsScene

I am trying to develop a project using qt but I have faced two problems both for adding items to a scene! I have a class containing my background object and it gets a pointer to my scene in its constructor.

  1. I tried "scene->addItem(this)" to add the background to the scene. however, while running the project, it is reported that the item has already been added to the scene! here is the only place I invoke addItem.

  2. I am also trying to make new objects of a few classes and put them inside a QList. While adding them, the items do not appear on the scene at all!

Here is the class:

class Test : public QObject, public QGraphicsPixmapItem{
    Q_OBJECT
public:
    Test(QGraphicsScene *s){
        scene = s;
        setPixmap(QPixmap("a.jpg"));
        setPos(0, 0);
        scene->addItem(this);
    }
    void mousePressEvent(QGraphicsSceneMouseEvent *event){
        list.push_back(new A(QPixmap("b.png")));
        scene->addItem(list.back());
    }
private:
    QGraphicsScene *scene;
}

P.S. A is class inheriting B which itself, inherits public QObject and public QGraphicsPixmapItem. The list also contains a couple of objects from type(B *).

Upvotes: 1

Views: 659

Answers (1)

nayana
nayana

Reputation: 3932

I would do this, no time to check if it compiles etc.. take it as half answer - but still it can help you..

Your problems:

  1. do not contain scene in your Test class - this is architectionally wrong, you can do this elsewhere in higher logic..
  2. also keep the list outside of Test class.. or maybe I do not understand your intention
  3. always call parents constructors in your constructor.. there are various reasons - be aware that when you have one parameter it can be inproperly converted and call the parent constructor etc.. maybe this caused problems in your constructor which had parameter of pointer to scene which maybe was passed to QObject construcor (then use explicit keyword - its much safer then) - these things are tricky.. when you have multiple inheritance I would advise always call the parent constructors manually as I did - also check this
  4. Why do you inherit from QObject? keep your logic out of graphics classes.. I think there is a way how to handle the mouse click event without signals (but I am lazy now to search).

--

class Test : public QObject, public QGraphicsPixmapItem 
{
Q_OBJECT
public:
    Test(QObject *qparent = 0, QGraphicsItem *parent = 0) 
    : QObject(qparent)
    , QGraphicsPixmapItem(parent) {
        setPixmap(QPixmap("a.jpg"));
        setPos(0, 0);
    }
    void mousePressEvent(QGraphicsSceneMouseEvent *event) {
        emit mousePressed();
    }
signals:
    void mousePressed();
}

Then somewhere in your window class:

WindowClass(etc) : Parent(etc) { //constructor
    QGraphicsScene *scene = new ....;
    Test *test = new Test(this, 0);//can be better, lazy to think of the details
    connect(test, SIGNAL(mousePressed()), this, SLOT(on_testMousePressed());
    scene->addItem(test);
}

void on_testMousePressed() {
    list.push_back(new A(QPixmap("b.png")));
    scene->addItem(list.back());        
}

Upvotes: 0

Related Questions