Home > Back-end >  Qt5.9.4 the same location, the view can obtain primitives, sence cannot obtain the primitives
Qt5.9.4 the same location, the view can obtain primitives, sence cannot obtain the primitives

Time:09-29

Sence of setSceneRect function called set size of (0,0,600,400); Inheriting QGraphicsItem, draw a straight line (0, 0) to (200200); In order to accurately select line, overloading the shape function,
But when the mouse to click, QGraphicsView call item () function can always return to the primitive, but using QGraphicsSence call ItemAt () function returns null pointer really (some of the same location, the view can obtain figure yuan, sence cannot obtain the figure yuan),

Const QRectF CLineItem: : boundingRect ()
{
Return shape (). ControlPointRect ();
}

Const QPainterPath CLineItem: : shape ()
{

QPainterPath path;
If (m_line==QLineF ())
Return path.

Path. The moveTo (m_line p1 ());
Path. The lineTo (m_line. P2 ());

QPainterPathStroker stoker.
Stoker. SetWidth (2);
Stoker. SetJoinStyle (Qt: : MiterJoin);
Stoker. SetCapStyle (Qt: : RoundCap);
Stoker. SetDashPattern (Qt: : DashLine);
Return stoker. CreateStroke (path);

}

Void XXXGraphicsView: : mousePressEvent (QMouseEvent * event)
{

If (event - & gt; Button ()==Qt: : LeftButton)
{
QPoint point=event - & gt; Pos ();
for(int i=0; i<601; + + I)
{
for(int j=0; j<401; + + j)
{
QGraphicsItem * item1=itemAt (QPoint (I, j));
If (item1)
{
QDebug () & lt; <" The view "& lt; }

QPointF pt=mapToScene (QPoint (I, j));
Item2 QGraphicsItem *=m_pScene - & gt; ItemAt (pt, the transform ());
If (item2)
{
QDebug () & lt; <" Sence "& lt; }
}
}
}
QGraphicsView: : mousePressEvent (event);
}
  • Related