February 14, 2011

Andrea Mugnaini Andrea Mugnaini
Lab Rat
10 posts

Hud and QGraphicsView

 

How to implement the hud (ie: life, high score, level,…), in a game where the qgraphicsview follows a qgraphicsitem of qgraphicscene?
I need to anchor a qgraphicstextitem (or label, …) so that it does not scroll with the qgraphicscene.
Would make sense to anchor the hud to qgraphicsview?

Suggestions or thoughts?

Many thanks.

10 replies

February 14, 2011

kkrzewniak kkrzewniak
Lab Rat
218 posts

Have you tried using custom some widgets with the graphicsview as their parent. And than setting their positions in the graphicsviews resize event.

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

February 14, 2011

Andre Andre
Area 51 Engineer
6031 posts

Doesn’t the QGraphicsItem::ItemIgnoresTransformations flag provide that functionality?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

February 14, 2011

Andrea Mugnaini Andrea Mugnaini
Lab Rat
10 posts

- If i add a qwidget on top of qgraphicsview, i note some problems with alpha channel of widget’s background.

furthermore

- if i set the flag of QGraphicsProxyWidget (got after calling addWidget on qgraphicscene) with ItemIgnoresTransformations, nothing happens.

A Solution could be: fix the qgraphicsview on qgraphicsscene, then scroll manually the content of qgraphicscene.

February 15, 2011

Andrea Mugnaini Andrea Mugnaini
Lab Rat
10 posts

Strange,
if i setViewport with QWidget instead QGLWidget, it works (I can see a correct blending between the transparencies of the backgrounds).

Hence, recapping:

- I have a QWidget (my hud) with transparent background;

- My hud is a son of QGraphicsView;

- If the QGraphicsView has a viewport of type QWidget, i can see correct blending of backgrounds.

- If the QGraphicsView has a viewport of type QGLWidget, i see a black background for my Hud.

Comments?

Many Thanks.

February 15, 2011

marcoB marcoB
Ant Farmer
167 posts

for transparencies in openGL I think you can try this:

  1. qgl=new QGLWidget(new QGLContext(QGLFormat(QGL::Rgba | QGL::DoubleBuffer | QGL::AlphaChannel | QGL::HasOverlay)));
  2. setViewport(qgl);

and to retrieve the coordinates to draw your HUD sticky to a position, you have to calculate the mapped position relatively to the current scene. For example, if you want to draw it at coords (10,10) of QGraphicsView widget:

  1. pos=mapToScene(10,10);
  2. hud.setPos(pos);

hope this help

February 15, 2011

Andrea Mugnaini Andrea Mugnaini
Lab Rat
10 posts

First of all, thanks for the support.

I tried the suggestion above but, nothing happens.
Hence, i invite you to see with your eyes the problem,
you can download this:
TestHudView [sites.google.com]

you can try to uncomment(or hack) inside

  1. void ZGraphicsView::Init()

Many thanks.

February 15, 2011

marcoB marcoB
Ant Farmer
167 posts

I didn’t know why a transparent widget over a openGL GraphicsView isn’t transparent. I think some function like initGL and updateGL should be reimplemented correctly, but you can reimplement QGraphicsView::drawForeground [doc.qt.nokia.com] to positioning the “QGraphicsProxyWidget *hud” when needed as follow

  1. void ZGraphicsView::drawForeground ( QPainter * painter, const QRectF & rect )
  2. {
  3.     hud->setPos(mapToScene(100, 10));
  4. }
  5.  
  6. void ZGraphicsView::Init()
  7. {
  8.     //------------------- CHECK THIS! -------------------
  9. //    setViewport(new QWidget());
  10.     setViewport(new QGLWidget(new QGLContext(QGLFormat(QGL::Rgba | QGL::DoubleBuffer | QGL::AlphaChannel | QGL::HasOverlay))));
  11.     //---------------------------------------------------
  12.  
  13.     // Hud Settings (on QGraphicsView)
  14.     QLabel* label = new QLabel("HUD!", 0);
  15.     label->setStyleSheet("QLabel{background: transparent; color: red; font: 40px;}");
  16.     label->setFixedSize(100, 44);
  17.     label->move(0, 0);
  18.  
  19.     // Background settings (on QGraphicsScene)
  20.     ZGraphicsScene* scene = new ZGraphicsScene();
  21.     QGraphicsPixmapItem* myBackground = new QGraphicsPixmapItem(QPixmap(":/background.png"), 0);
  22.     scene->addItem(myBackground);
  23.     setScene(scene);
  24.     setFixedSize(myBackground->boundingRect().size().toSize());
  25.  
  26.     hud = scene->addWidget(label);
  27.     hud->setPos(mapToScene(100, 10));
  28.  
  29.     show();
  30. }

February 15, 2011

Andrea Mugnaini Andrea Mugnaini
Lab Rat
10 posts
MarcoB wrote:
but you can reimplement QGraphicsView::drawForeground [doc.qt.nokia.com] to positioning the “QGraphicsProxyWidget *hud” when needed as follow

The Test that i added above it isn’t my true case.
In my case, i can’t do it, because (for now) i have the QGraphicsView that scrolls a QGraphicsScene, so if i move my hud to fixed position, whenever drawForeground is executed, i see that my hud flickers with scrolling.

Many Thanks.

September 28, 2011

saddan saddan
Lab Rat
3 posts

Hi,

Did you solve the transparency problem with widgets added to the scene?

September 28, 2011

Andrea Mugnaini Andrea Mugnaini
Lab Rat
10 posts

Yes, i solved this problem (more or less).
You can find my game here:
http://sites.google.com/site/mugnaini81/Psycho-naut_source_code_v016.zip

but, now i am using another kind of solution for the hud cause i stopped to use the qgraphicsview system.

Basically I am developing a 3D shoot’ em up, using a QGLWidget with an hybrid system for the hud: ortographic projection+qfont+opengl commands.

 
  ‹‹ Set packet TTL with QT Network library      QTreeView & mapToGlobal(...): Is this considered a bug? ››

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