Tharanga
Tharanga

Reputation: 2097

Extending QGraphicsScene

I am extending QGraphicsItem to be added to a extended QGraphicsSene. when I added the extended item to the scene and the scene to the graphics view in the normal way it shows the image but when I added the image as follows it does not show. could some one please check this out and tell me the issue.

header

#ifndef IMAGEMAP_H
#define IMAGEMAP_H

#include <QGraphicsItem>
#include <QGraphicsScene>

class ScanImage : public QGraphicsItem
{
public:
    ScanImage(const QString imgsrc);
    ~ScanImage();

    void setImageSource(const QString is);
    QString imageSource();

    QRectF boundingRect() const;
    void paint( QPainter *painter,
                const QStyleOptionGraphicsItem *option,
                QWidget *widget);

protected:
     void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);

private:
     QString imgsrc;
};

class ImageHolder : public QGraphicsScene
{
public:
    ImageHolder();
    ~ImageHolder();

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);

private:
    QRectF selectedRect;
};

#endif //

source

#include "imagemap.h"
#include "QtGui"

ScanImage::ScanImage(const QString is)
{
    imgsrc=is;
    update();
}

ScanImage::~ScanImage()
{
}

ImageHolder::ImageHolder()
{
    setSceneRect(0.0,0.0,512.0,512.0);
    ScanImage im("2.jpg");
    im.setZValue(1.0);
    im.setVisible(true);
    addItem(&im);
}

ImageHolder::~ImageHolder()
{
}

void ScanImage::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug() <<event->pos();
}

void ImageHolder::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug() <<event->scenePos().rx();
    selectedRect.setTopLeft(event->scenePos());
}

void ImageHolder::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    qDebug() <<mouseEvent->scenePos().ry();
    selectedRect.setBottomRight(mouseEvent->scenePos());
    addRect ( selectedRect);
}

QRectF ScanImage::boundingRect() const
{
    return QRectF(0.0, 0.0, 512.0, 512.0);
}

void ScanImage::paint( QPainter* painter,
                       const QStyleOptionGraphicsItem*,
                       QWidget* )
{
    QRectF target(0.0, 0.0, 512.0, 512.0);
    QRectF source(0.0, 0.0, 512.0, 512.0);
    painter->drawImage(target, QImage(imgsrc),source);
}

void ScanImage::setImageSource(QString is)
{
    imgsrc = is;
}

QString ScanImage::imageSource()
{
    return imgsrc;
}

main

int main(int argv, char* argc[])
{
    QApplication app(argv,argc);
    ImageHolder scene;
    QGraphicsView view(&scene);
    view.resize(512,512);
    view.show();
    return app.exec();
}

Upvotes: 1

Views: 1250

Answers (1)

Stephen Chu
Stephen Chu

Reputation: 12832

You are adding a QGraphicsItem allocated as a local variable on the QGraphicsScene constructor's stack. Once the constructor is finished, the objects on its stack are automatically deallocated (i.e. deleted) and in your case removed from the scene. Use new operator to create the item.

Upvotes: 2

Related Questions