如何在主窗口中响应点击QGraphicsItem?
各位,我的程序是这样的,在主窗口放置了QGraphicsView控件,并通过重写QGraphicsItem在画了多个item,现在的问题是我如果点击所画的item,该如何操作才能在主窗口中响应该点击,现在能做到的仅仅只能在QGraphicsItem响应各种鼠标操作,该如何反映到主窗口中?看网上的资料要重写QGraphicsView的鼠标事件,但是试了好久依然没有成功,请教各位该如何操作?谢谢各位了!
代码如下:
class VpOffest : public QMainWindow
{
Q_OBJECT
public:
VpOffest(QWidget *parent = 0);
~VpOffest();
public:
void paintEvent(QPaintEvent *event);
public:
QGraphicsScene *scene;
MyItem *itemPic;
private:
Ui::VpOffestClass ui;
};
VpOffest::VpOffest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
void VpOffest::paintEvent(QPaintEvent *event)
{
if (!ifSceneEmpty)
{
scene->destroyed();
drawPointToDlg();
}
}
int VpOffest::drawPointToDlg()
{
scene = new QGraphicsScene(this);
scene->setSceneRect(0, 0, ui.g_grafic->geometry().width(), ui.g_grafic->geometry().height());
………
itemPic = new MyItem(xVal, yVal);
itemPic->setPos(xVal, yVal);
scene->addItem(itemPic);
}
ui.g_grafic->setScene(scene);
ifSceneEmpty = false;
return 0;
}
class MyItem : public QGraphicsItem
{
Q_OBJECT
public:
MyItem(int, int);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
public:
int xVal, yVal;
QGraphicsItem *currentItem;
QGraphicsItem *lastItem;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};
#endif // MYITEM_H
MyItem::MyItem(int x, int y)
{
this->xVal = x;
this->yVal = y;
setFlag(QGraphicsItem::ItemIsFocusable);
setFlag(QGraphicsItem::ItemIsSelectable);
}
QRectF MyItem::boundingRect() const
{
return QRectF(0, 0, 15 * globalPara::zoomScale, 15 * globalPara::zoomScale);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
QRectF rect = boundingRect();
painter->drawEllipse(0, 0, 15 * globalPara::zoomScale, 15 * globalPara::zoomScale);
}
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
currentItem = QGraphicsItem::focusItem();
int x = currentItem->x();
int y = currentItem->y();
QGraphicsItem::mousePressEvent(event);
}