Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Hud and QGraphicsView

Hud and QGraphicsView

Scheduled Pinned Locked Moved General and Desktop
12 Posts 6 Posters 11.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrea Mugnaini
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kkrzewniak
      wrote on last edited by
      #2

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

      Me, Grimlock, not "nice dino". ME BASH BRAINS!

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Andrea Mugnaini
          wrote on last edited by
          #4
          • 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.
          1 Reply Last reply
          0
          • A Offline
            A Offline
            Andrea Mugnaini
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              deimos
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Andrea Mugnaini
                wrote on last edited by
                #7

                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":https://sites.google.com/site/mugnaini81/TestHudView.zip?attredirects=0&d=1

                you can try to uncomment(or hack) inside @void ZGraphicsView::Init()@

                Many thanks.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  deimos
                  wrote on last edited by
                  #8

                  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":http://doc.qt.nokia.com/latest/qgraphicsview.html#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);
                  scene->addItem(myBackground);
                  setScene(scene);
                  setFixedSize(myBackground->boundingRect().size().toSize());
                  
                  hud = scene->addWidget(label);
                  hud->setPos(mapToScene(100, 10));
                  
                  show();
                  

                  }@

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Andrea Mugnaini
                    wrote on last edited by
                    #9

                    [quote author="MarcoB" date="1297775701"] but you can reimplement "QGraphicsView::drawForeground":http://doc.qt.nokia.com/latest/qgraphicsview.html#drawForeground to positioning the "QGraphicsProxyWidget *hud" when needed as follow
                    [/quote]

                    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.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      saddan
                      wrote on last edited by
                      #10

                      Hi,

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

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Andrea Mugnaini
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Alain38
                          wrote on last edited by
                          #12

                          As I found this topic searching another similar problem, just a way to have a transparent widget on an OpenGL background:
                          @widget->setWindowOpacity(0.95);
                          widget->setAttribute(Qt::WA_TranslucentBackground);@

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

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved