Note that there are some explanatory texts on larger screens.

plurals
  1. POQt tracing QGraphicsScene::itemAt() back to data model
    primarykey
    data
    text
    <p>I have a list of objects that I use to add objects into a QGraphicsScene:</p> <pre><code>for(int i = 0; i &lt; levelObjects.length(); i++) { QRect objRect; objRect = spriteSheetLocations.value(levelObjects.at(i).value("frame_name")); //Q_ASSERT_X(objRect != QRect(0,0,0,0), "MainWindow::loadFile()", "Could not find sprite location!"); QImage img = spriteSheet.copy(objRect); int height = levelObjects.at(i).value("height").toInt(); int width = levelObjects.at(i).value("width").toInt(); int x = levelObjects.at(i).value("x").toInt(); int y = levelObjects.at(i).value("y").toInt(); img = img.scaled(QSize(width, height), Qt::IgnoreAspectRatio); item = scene-&gt;addPixmap(QPixmap::fromImage(img)); int xPos = x - width/2; int yPos = levelPlist.value("level_height").toInt() - (y + height/2); item-&gt;setPos(xPos, yPos); } </code></pre> <p>Later on, in the GraphicsScene class, I detect when the user clicks on an item and drags it to move it:</p> <pre><code>void LevelGraphicsView::mousePressEvent(QMouseEvent *event) { if (QGraphicsItem *item = itemAt(event-&gt;pos())) { qDebug() &lt;&lt; "You clicked on item" &lt;&lt; item; draggedItem = item; int mouseX = draggedItem-&gt;pos().x() - mapToScene(event-&gt;pos()).x(); int mouseY = draggedItem-&gt;pos().y() - mapToScene(event-&gt;pos()).y(); mouseOffset = QPointF(mouseX, mouseY); } else { qDebug() &lt;&lt; "You didn't click on an item."; draggedItem = NULL; mouseOffset = QPointF(0,0); } } void LevelGraphicsView::mouseMoveEvent(QMouseEvent *event) { if(!draggedItem) // no item selected return; QPointF pos = mapToScene(event-&gt;pos()) + mouseOffset; draggedItem-&gt;setPos(pos); } </code></pre> <p>This works fine for moving the items in the graphics view, but I'm having trouble tracing the QGraphicsItem back to the list item that created it.</p> <p>What's the best way to link the QGraphicsItem with the list item from which it was made so that the list item can be changed to reflect the change of position?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload