呼叫Sence的setSceneRect函式設定大小為(0,0,600,400);實作繪制直線(0,0)到(200,200),為了精確選擇直線,多載了shape函式,呼叫qDebug輸出圖元,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(0,0);
path.lineTo(200,200);
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/yidong/48856.html
標籤:Qt
上一篇:QT&opencv顯示不了圖片
下一篇:象app:layout_constraintEnd_toEndOf 以app:開頭的屬性在官方檔案的什么位置可以查到??
