Qt 使用鼠标按下事件添加pixmap
我想将pixmap项目添加到具有鼠标按下事件的场景中,但它不显示任何内容。 这是我的班级:Qt 使用鼠标按下事件添加pixmap,qt,mouseevent,qgraphicsscene,qpixmap,Qt,Mouseevent,Qgraphicsscene,Qpixmap,我想将pixmap项目添加到具有鼠标按下事件的场景中,但它不显示任何内容。 这是我的班级: #include <QGraphicsScene> #include <QGraphicsPixmapItem> #include <QGraphicsItem> #include <QRectF> #include <QPainter> #include <QPointF> #include
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QRectF>
#include <QPainter>
#include <QPointF>
#include "chessscene.h"
class knight:public QGraphicsPixmapItem
{
public:
knight();
virtual QRectF boundingRect();
void target();
void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
knight *currentknight;
};
在我的棋盘构造器中,它正在工作,但不在这里。
对不起,我的英语和代码。我刚开始学习C++和QT和英语。在Kn::目标,你正在堆栈上创建一个新的QGoCICICFIZE,添加一个像素映射。当您离开此范围时,此场景及其所有项目将被销毁。你应该在->场景中添加你的新物品,这将指定你当前的骑士实例这就是答案。。谢谢你这么快的回复。
#include "knight.h"
#include "chessscene.h"
#include "board.h"
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QDebug>
#include <QRectF>
#include <QPointF>
#include <QGraphicsSceneMouseEvent>
knight::knight()
{
setFlag(ItemIsSelectable);
}
QRectF knight::boundingRect()
{
return QRectF (0,0,140,140);
}
void knight::target()
{
QGraphicsPixmapItem *item=new QGraphicsPixmapItem;
QPixmap pix(":/images/target.png");
item->setPixmap(pix);
chessscene _scene;
_scene.basescene->addItem(item);
qDebug() <<"A";
}
void knight::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
target();
setFlag(ItemIsMovable);
QGraphicsPixmapItem::mousePressEvent(event);
}
QGraphicsPixmapItem *item=new QGraphicsPixmapItem;
QPixmap pix(":/images/target.png");
item->setPixmap(pix); chessscene _scene;
_scene.basescene->addItem(item);