2012-12-24 56 views
11

我創建了一個自定義QGraphicsItem。並覆蓋boundingRect()paint()什麼是qtransform在QGraphicsScene :: itemAt()

QRectF myTile::boundingRect() const 
{ 
    return QRectF(xPos*10, yPos*10, 10, 10); 
} 

void myTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
{ 
    QRectF rec = boundingRect(); 
    int gvi = value * 255; 
    QColor gv(gvi, gvi, gvi, 255); 
    QBrush brush(gv); 
    painter->fillRect(rec, brush); 
    painter->drawRect(rec); 
} 

然後我用addItem()的項目添加到場景。現在我想通過它的位置從現場得到它。我找到itemAt函數。但問題是我不知道什麼是const QTransform & deviceTransform。我應該如何使用QTransform

因爲我沒有在QGraphicsItem中實施任何轉換。這讓我困惑。

回答

4
QGraphicsItem * QGraphicsScene::itemAt (const QPointF & position, const QTransform & deviceTransform) const 

返回最頂部可見的項目在指定位置,或0,如果 有在此位置沒有任何項目。 deviceTransform是適用於視圖的 轉換,如果場景包含忽略轉換的項目,則需要提供 轉換。在Qt 4.6中引入了此功能 。

所以,我要說,如果你有需要改變一些項目而忽略其他,你可以簡單地用QTransform()默認值,甚至更好的QGraphicsView::transform() const去。

soo long zai

+0

雖然QGraphicsScene可以有多個視圖,所以'deviceTransform'的值取決於上下文。例如,在鼠標事件處理程序中,您必須弄清事件來自哪個視圖。請參閱[此評論](http://stackoverflow.com/questions/16919819/itemat-not-returning-custom-qgraphicsitem#comment24426364_16921083)。 –

相關問題