Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsView/QGraphicsScene



  • I just started learning how to display images to the User. In reading through the documentation it seems that the QGraphicsView and QGraphicsScene offer the best approach. Basically, I want to a display a map to the User in the main Window and then overlay dialog boxes containing controls and/or other information to the user.

    In my first attempt to display the base image to the User it was displayed in repeating panels. I just want to display a single image centered on the screen. I've been reading through the documentation but so far haven't discovered how to center the image in the view.

    @
    QGraphicsScene scene;
    scene.setSceneRect(-300, -300, 600, 600);
    scene.setItemIndexMethod(QGraphicsScene::NoIndex);

    QGraphicsView view(&scene);
    view.setRenderHint(QPainter::Antialiasing);
    view.setBackgroundBrush(QPixmap(":/Game/DataFiles/map.jpg"));
    view.setCacheMode(QGraphicsView::CacheBackground);
    view.setAlignment(Qt::AlignCenter);
    view.setContentsMargins(QMargins::QMargins(5, 5, 5, 5));
    view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Map Test"));
    view.resize(900, 700);
    view.show();
    

    @



  • If you want a single image, AFAIK, you'll need to use QGraphicsItem (or QGraphicsWidget). Setting the image as a background brush will, as you've observed, result in it being tiled, because it is painted across the entirety of the background.


Log in to reply