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
- 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.
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.
for transparencies in openGL I think you can try this:
- qgl=new QGLWidget(new QGLContext(QGLFormat(QGL::Rgba | QGL::DoubleBuffer | QGL::AlphaChannel | QGL::HasOverlay)));
- 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:
- pos=mapToScene(10,10);
- hud.setPos(pos);
hope this help
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
- void ZGraphicsView::Init()
Many thanks.
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
- {
- hud->setPos(mapToScene(100, 10));
- }
- void ZGraphicsView::Init()
- {
- //------------------- CHECK THIS! -------------------
- // setViewport(new QWidget());
- setViewport(new QGLWidget(new QGLContext(QGLFormat(QGL::Rgba | QGL::DoubleBuffer | QGL::AlphaChannel | QGL::HasOverlay))));
- //---------------------------------------------------
- // Hud Settings (on QGraphicsView)
- label->setStyleSheet("QLabel{background: transparent; color: red; font: 40px;}");
- label->setFixedSize(100, 44);
- label->move(0, 0);
- // Background settings (on QGraphicsScene)
- ZGraphicsScene* scene = new ZGraphicsScene();
- scene->addItem(myBackground);
- setScene(scene);
- setFixedSize(myBackground->boundingRect().size().toSize());
- hud = scene->addWidget(label);
- hud->setPos(mapToScene(100, 10));
- show();
- }
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.
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.
You must log in to post a reply. Not a member yet? Register here!




