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. QPixmap and QGraphicsScene
Qt 6.11 is out! See what's new in the release blog

QPixmap and QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 6.8k Views 2 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.
  • L Lunarix

    That should be all of it. I call scene->addPolygon three times with different hexpoints so this should draw all 3 hexagons

    A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #4

    @Lunarix said in QPixmap and QGraphicsScene:

    That should be all of it. I call scene->addPolygon three times with different hexpoints so this should draw all 3 hexagons

    Sorry, I missed the scroll bar.

    The way you do it, you apply your pixmap as a texture to your object. How does Qt decide where to place the texture? Simple: By default, the top-left of your pixmap starts at the position - pos() - of your GraphicsItem.

    All your polygons have a default position, which is 0,0. They draw in different places, because you add the polygon points elsewhere. Think of your polygons like three sheets of paper: On the first sheet, you draw the polygon in the top left corner. On the second sheet to draw the polygon further to the right. And on the third sheet, you draw it further down, and somewhat to the right. Then you stack the sheets exactly on top of each other. That means that the origin of the texture is exactly the same.

    Solution:

    • Create only one QPolygonF (this one: QPointF(70,20) <<QPointF(35,0) <<QPointF(0,20) << QPointF(0,50) <<QPointF(35,70) <<QPointF(70,50))
    • Apply the same QPolygonF to all your GraphicsItems, but move them where you want using setPos
    1 Reply Last reply
    2
    • L Offline
      L Offline
      Lunarix
      wrote on last edited by
      #5

      @Asperamanca
      thank you for your answer.
      Currently I can not find any function "setPos".

              hexPoints << QPointF(70,20) <<QPointF(35,0) <<QPointF(0,20) << QPointF(0,50) <<QPointF(35,70) <<QPointF(70,50);
              QPolygonF my_polygon(hexPoints);
              scene->addPolygon(my_polygon,outlinePen,*Brush_wood);
      
              // scene->addPolygon(my_polygon,outlinePen,*Brush_ore); this right from the first hex
              // scene->addPolygon(my_polygon,outlinePen,*Brush_wood); this at the lower right side
      
              ui->graphicsView->setScene(scene);
      

      So now I have only one Polygon now. But how do I move it? Neither QPolygonF, QGraphicsView nor QGraphicsScene have a function setPos.
      Thank you for your help :)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #6

        Hi,

        addPolygon returns a pointer to a QGraphicsPolygonItem. That's the element you want to call setPos on.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • L Offline
          L Offline
          Lunarix
          wrote on last edited by
          #7

          Helo @SGaist,
          So I added this code:

                  QGraphicsPolygonItem * my_poly = new QGraphicsPolygonItem;
                  my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood);
                  my_poly->setPos(70,70);
            
                 //  scene->addPolygon(my_poly,outlinePen,*Brush_wood);
                 // ui->graphicsView->setScene(scene);
          
          

          But now - who to add the QGraphicsPolygonItem to the ui->graphicsView->xxx?
          Or do I have to add it to the scene again?
          Thank you for your help

          jsulmJ 1 Reply Last reply
          0
          • L Lunarix

            Helo @SGaist,
            So I added this code:

                    QGraphicsPolygonItem * my_poly = new QGraphicsPolygonItem;
                    my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood);
                    my_poly->setPos(70,70);
              
                   //  scene->addPolygon(my_poly,outlinePen,*Brush_wood);
                   // ui->graphicsView->setScene(scene);
            
            

            But now - who to add the QGraphicsPolygonItem to the ui->graphicsView->xxx?
            Or do I have to add it to the scene again?
            Thank you for your help

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @Lunarix said in QPixmap and QGraphicsScene:

            QGraphicsPolygonItem * my_poly = new QGraphicsPolygonItem;

            Why do you create a new instance here? It is not needed and you leak memory. Just do:

            QGraphicsPolygonItem *my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood);
            my_poly->setPos(70,70);
            

            "But now - who to add the QGraphicsPolygonItem to the ui->graphicsView->xxx?
            Or do I have to add it to the scene again?" - isn't it already added to the scene if you call scene->addPolygon?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            L 1 Reply Last reply
            1
            • jsulmJ jsulm

              @Lunarix said in QPixmap and QGraphicsScene:

              QGraphicsPolygonItem * my_poly = new QGraphicsPolygonItem;

              Why do you create a new instance here? It is not needed and you leak memory. Just do:

              QGraphicsPolygonItem *my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood);
              my_poly->setPos(70,70);
              

              "But now - who to add the QGraphicsPolygonItem to the ui->graphicsView->xxx?
              Or do I have to add it to the scene again?" - isn't it already added to the scene if you call scene->addPolygon?

              L Offline
              L Offline
              Lunarix
              wrote on last edited by
              #9

              @jsulm
              Your Right, but my_poly is not set to the scene yet.
              Currently I see only one hex, the one in the origin, but not the one which I moved

              jsulmJ 1 Reply Last reply
              0
              • L Lunarix

                @jsulm
                Your Right, but my_poly is not set to the scene yet.
                Currently I see only one hex, the one in the origin, but not the one which I moved

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @Lunarix Is my_poly visible?
                From documentation (http://doc.qt.io/qt-5/qgraphicsscene.html#addPolygon):
                "If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.".

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • L Offline
                  L Offline
                  Lunarix
                  wrote on last edited by
                  #11

                  @jsulm
                  No it is not.
                  alt text

                  That is the result atm.

                          hexPoints << QPointF(70,20) <<QPointF(35,0) <<QPointF(0,20) << QPointF(0,50) <<QPointF(35,70) <<QPointF(70,50);
                          QPolygonF my_polygon(hexPoints);
                           QGraphicsPolygonItem * my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood);
                          my_poly->setPos(70,70);
                          ui->graphicsView->setScene(scene);
                  

                  and this is the code. The current hex is stored in scene and this is viewed, so i think my_poly has no influence yet.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Lunarix
                    wrote on last edited by
                    #12
                            ui->graphicsView->setScene(scene);
                            my_poly->setPos(70,70);
                    

                    works now. Because, as you said, it has to be shown before.
                    If I'd like to add 50+ hex, I have to make 50+ QGraphicsPolygonItem? Because it seems like one QGraphicsPolygonItem can change position of one item.

                    jsulmJ 1 Reply Last reply
                    0
                    • L Lunarix
                              ui->graphicsView->setScene(scene);
                              my_poly->setPos(70,70);
                      

                      works now. Because, as you said, it has to be shown before.
                      If I'd like to add 50+ hex, I have to make 50+ QGraphicsPolygonItem? Because it seems like one QGraphicsPolygonItem can change position of one item.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #13

                      @Lunarix Change x, y and my_polygon as needed inside the loop

                      for (int i = 0; i < 50); ++i) {
                          QGraphicsPolygonItem *my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood);
                          my_poly->setPos(x,y);
                      }
                      

                      Yes, each polygon is a polygon item, but you can reuse my_poly variable.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      3
                      • L Offline
                        L Offline
                        Lunarix
                        wrote on last edited by
                        #14

                        Works! Thanks a lot to everyone of you :)
                        Have a nice day

                        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