QGraphicsItem use Precautions

When I was writing the zoomed picture function with qt (see https://blog.csdn.net/weixin_43935474/article/details/89327314) I placed a QGraphicsView on the main interface, and then put inside QGraphicsScene, then himself rewrote QGraphicsItem class and placed in QGraphicsScene to display pictures and zoom in and out. There two troubled for a long time, now finally solved:
First, how to get qgraphicview in qgraphicitem news outside?
To do:
1. rewrite QGraphicsItem class, first inherited Qobject, then inherited qgraphicview, otherwise it will error, as follows :( I rewrite QGraphicsItem called ImageWidget)

class ImageWidget :public QObject, public QGraphicsItem
{
    Q_OBJECT
    ······

2. Add signales

signals:
    void    showPos(QPointF pointf, QColor qColor,qreal qrScale);

3. Add response function signal and connect the main interface:

QObject::connect(m_Image,&ImageWidget::showPos,this,&MainW::showMousePos);//该行代码写在主界面类的构造函数中
void MainW::showMousePos(QPointF point, QColor qColor, qreal qrScale)
{
	//TODO:
}

Two, QGraphicsItem obtain mouse movement within the class of messages
beginning with:

void ImageWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)

Later found in case the user does not click a mouse can not trigger the function.
Then switch to:

void ImageWidget::hoverMoveEvent(QGraphicsSceneHoverEvent *event)

On it, and add the following code to the main interface function:

m_Image->setAcceptHoverEvents(true);//m_Image是ImageWidget对象
m_graphicsScene->addItem(m_Image);//m_graphicsScene是QGraphicsScene对象
m_GraphicView->setScene(m_graphicsScene);//m_GraphicView是QGraphicsView对象
m_GraphicView->setMouseTracking(true);

Guess you like

Origin blog.csdn.net/weixin_43935474/article/details/89917667