呼叫Sence的setSceneRect函式設定大小為(0,0,600,400);繼承QGraphicsItem,實作繪制直線(0,0)到(200,200);為了精確選擇直線,多載了shape函式,
但當滑鼠點擊時,QGraphicsView呼叫item()函式總是能回傳圖元,但使用QGraphicsSence呼叫ItemAt()函式確回傳為空指標(某些相同的位置,view能獲得圖元,sence不能獲得圖元)。
QRectF CLineItem::boundingRect() const
{
return shape().controlPointRect();
}
QPainterPath CLineItem::shape() const
{
QPainterPath path;
if (m_line == QLineF())
return path;
path.moveTo(m_line.p1());
path.lineTo(m_line.p2());
QPainterPathStroker stroker;
stroker.setWidth(2);
stroker.setJoinStyle(Qt::MiterJoin);
stroker.setCapStyle(Qt::RoundCap);
stroker.setDashPattern(Qt::DashLine);
return stroker.createStroke(path);
}
void XXXGraphicsView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
QPoint point= event->pos();
for(int i=0; i<601; ++i)
{
for(int j=0; j<401; ++j)
{
QGraphicsItem* item1 = itemAt(QPoint(i,j));
if(item1)
{
qDebug()<<"view"<<QPoint(i,j)<<item1; /// 1.列印在該位置view能獲取的圖元
}
QPointF pt = mapToScene(QPoint(i,j));
QGraphicsItem* item2 = m_pScene->itemAt(pt,transform());
if(item2)
{
qDebug()<<"sence"<<QPoint(i,j)<<item2; /// 2.列印在該位置sence能獲取的圖元(比1少)
}
}
}
}
QGraphicsView::mousePressEvent(event);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/97378.html
標籤:工具平臺和程序庫
上一篇:新手一個,這個程式怎么寫啊
