Reputation: 543
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.
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.
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
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:
--
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