Search code examples
c++qtqgraphicsviewqgraphicsscene

Place and image in specific coordinates in QGraphicsScene


In the code below I have used item.setPos(0,0); in order to place green.png in the upper-left corner of my application (QGraphicsView actually). But is appears right in the center from both directions - vertically and horizontally. Please help to put the image in 0,0 coordinates.

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QIcon>



int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QGraphicsScene scene;
    scene.setBackgroundBrush(QBrush(QColor(255, 255, 255), Qt::SolidPattern));


    QGraphicsPixmapItem item(QPixmap(":/images/green.png"));
    item.setPos(0,0);
    scene.addItem(&item);


    QGraphicsView view(&scene);
    view.setFixedSize(400, 400);
    view.setWindowTitle("Diamond Dash");
    view.setWindowIcon(QIcon(":/images/red.png"));
    view.show();

    return app.exec();
}

Solution

  • I have added

    scene.setSceneRect(0,0, 400, 400);
    

    and it worked!