[英]function declared in my class is not showing in scope
在我的.h中:
class ImagePixmapItem: public QGraphicsPixmapItem
{
public:
void setSize(qreal size);
private:
qreal size;
};
在我的班级文件中,我现在有两个功能:
#include "imagepixmapitem.h"
#include <QGraphicsSceneWheelEvent>
ImagePixmapItem::ImagePixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem)
: QGraphicsPixmapItem(pixmap,parentItem)
{
setCacheMode(NoCache);
}
ImagePixmapItem::~ImagePixmapItem()
{
}
void ImagePixmapItem::setSize(qreal size)
{
this->size = size;
}
void wheelEvent ( QGraphicsSceneWheelEvent * event ){
qreal size = 1.2;
if (event->delta() < 0)
size = 1.0 / size;
setSize(size);
}
但是我在setSize上遇到错误:
../IMViewer/imagepixmapitem.cpp:42:错误:未在此范围内声明'setSize'
我在这里做错了什么?
void wheelEvent ( QGraphicsSceneWheelEvent * event )
应该说
void ImagePixmapItem::wheelEvent ( QGraphicsSceneWheelEvent * event )
并将其添加到.h
protected:
void wheelEvent ( QGraphicsSceneWheelEvent * event );
wheelEvent
是一个免费的功能,因此不能调用成员函数ImagePixmapItem::setSize
而不的实例ImagePixmapItem
。
void wheelEvent ( QGraphicsSceneWheelEvent * event )
不是ImagePixmapItem
类的成员,而setSize()是。 因此,要调用setSize(),您需要将wheelEvent()设置为ImagePixmapItem
类的成员,或者传递类型为ImagePixmapItem
的对象,然后调用ImagePixmapItemObject.setSize( ... )
。
这是因为setSize
没有在wheelEvent
函数的范围内声明。 函数setSize
是ImagePixmapItem
类的成员函数,因此要调用setSize
,需要该类的实例。
WheelEvent-function在前面缺少ImagePixMapItem ::,这意味着您实际上需要从QGraphicsPixmapItem类覆盖该函数
所以应该有
在标题中:
protected:
virtual void wheelEvent(QGraphicsSceneWheelEvent *event)
和在cpp
void ImagePixmapItem::wheelEvent(QGraphicsSceneWheelEvent *event)
还应注意,由于您覆盖了WheelEvent,因此从不调用QGraphicsItem-class中的实现,因此也应调用该函数的基类实现:
QGraphicsPixmapItem::wheenEvent(event);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.