January 25, 2011

cazador7907 cazador7907
Lab Rat
78 posts

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.

  1.     QGraphicsScene scene;
  2.     scene.setSceneRect(-300, -300, 600, 600);
  3.     scene.setItemIndexMethod(QGraphicsScene::NoIndex);
  4.  
  5.     QGraphicsView view(&scene);
  6.     view.setRenderHint(QPainter::Antialiasing);
  7.     view.setBackgroundBrush(QPixmap(":/Game/DataFiles/map.jpg"));
  8.     view.setCacheMode(QGraphicsView::CacheBackground);
  9.     view.setAlignment(Qt::AlignCenter);
  10.     view.setContentsMargins(QMargins::QMargins(5, 5, 5, 5));
  11.     view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Map Test"));
  12.     view.resize(900, 700);
  13.     view.show();

 Signature 

Laurence -

 

1 reply

January 25, 2011

Robin Burchell Robin Burchell
Lab Rat
21 posts

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.

 Signature 


http://rburchell.com

 
  ‹‹ QSqlRelationalTableModel woes when selecting from a view      run application in background and show icon in menubar and pop up menu in finder(mac os) ››

You must log in to post a reply. Not a member yet? Register here!