扩展 QGraphicsScene

发布于 2024-11-09 07:06:05 字数 2629 浏览 0 评论 0原文

我正在扩展 QGraphicsItem 以将其添加到扩展的 QGraphicsSene 中。当我以正常方式将扩展项目添加到场景并将场景添加到图形视图时,它会显示图像,但是当我按如下方式添加图像时,它不会显示。有人可以检查一下并告诉我这个问题吗?

标头

#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 //

#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;
}

主要

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

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2024-11-16 07:06:05

您将添加一个分配为 QGraphicsScene 构造函数堆栈上的局部变量的 QGraphicsItem。一旦构造函数完成,其堆栈上的对象将自动释放(即删除),并在您的情况下从场景中删除。使用 new 运算符创建项目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文