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.

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

  • Doesn't the QGraphicsItem::ItemIgnoresTransformations flag provide that functionality?

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


    • 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.


    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)));

    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:

    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:

    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": to positioning the "QGraphicsProxyWidget *hud" when needed as follow

    @void ZGraphicsView::drawForeground ( QPainter * painter, const QRectF & rect )
    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)
    QLabel* label = new QLabel("HUD!", 0);
    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();
    QGraphicsPixmapItem* myBackground = new QGraphicsPixmapItem(QPixmap(":/background.png"), 0);
    hud = scene->addWidget(label);
    hud->setPos(mapToScene(100, 10));


  • [quote author="MarcoB" date="1297775701"] but you can reimplement "QGraphicsView::drawForeground": 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.

  • Hi,

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

  • Yes, i solved this problem (more or less).
    You can find my game here:

    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.

  • As I found this topic searching another similar problem, just a way to have a transparent widget on an OpenGL background:

    The opacity value is just to see what you put in the widget. The only forbidden value is 1.0.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.