noonana
noonana

Reputation: 13

Crash when adding QGraphicsProxyWidget

I'm trying to have a QPushButton in my scene, but when I'm trying to add the QGraphicsProxyWidget to the scene, it crashes.

So here's the .cpp:

#include "upgradecromagnon.h"
#include "game.h"
#include <QGraphicsProxyWidget>
#include <qDebug>

extern Game *game;
UpgradeCromagnon::UpgradeCromagnon()
{

    this->setRect(-50,0,150,50);
    buttonAmelio = new QPushButton("salut");
    teste();
}

void UpgradeCromagnon::teste()
{
    QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
    proxy->setWidget(buttonAmelio);
    scene()->addItem(proxy);
}

and its .h:

#ifndef UPGRADECROMAGNON_H
#define UPGRADECROMAGNON_H
#include <QPainter>
#include <QGraphicsRectItem>
#include <QPushButton>
class UpgradeCromagnon: public QGraphicsRectItem
{
public:
    UpgradeCromagnon();
    void teste();
private:
    QPushButton *buttonAmelio;
};

#endif // UPGRADECROMAGNON_H

Upvotes: 0

Views: 132

Answers (1)

G.M.
G.M.

Reputation: 12899

Your UpgradeCromagnon constructor calls UpgradeCromagnon::teste which, in turn, calls QGraphicsItem::scene. At that point QGraphicsItem::scene must return a null pointer since there's no possible way the UpgradeCromagnon instance can have been added to a QGraphicsScene before its contructor has completed (not according to the code you've provided at any rate).

Upvotes: 1

Related Questions