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. how to draw QGraphicsPolygonItem
Forum Updated to NodeBB v4.3 + New Features

how to draw QGraphicsPolygonItem

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 1.8k 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by
    #1

    Hi,

    I am trying to draw a polygon in my graphicsScene.
    The polygon exists of QPoints that are stored inside qv_points (qv = QVector)

    void graphicsView::addPoly()
    {
        qDebug() << "Add polygon";
    
        QPolygonF polygon;
        polygon << qv_points;
    
        QBrush brush;
        brush.setColor(GREEN);
    
        QGraphicsPolygonItem *newPoly = new QGraphicsPolygonItem();
        newPoly->setPolygon(polygon);
        newPoly->setBrush(QBrush(GREEN, Qt::SolidPattern));
        scene->addItem(newPoly);
    }
    

    It comes into the function since I can see "Add polygon" in the debugger.
    Does anyone know why my polygon is not being drawn?

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #12

      hi
      The debugger for visual studio is a separate download called
      Debugging tools from MS.

      When you press Ctrl+N , you are not creating a new instance of the
      graphicsView or something like that ?

      H 1 Reply Last reply
      0
      • H hobbyProgrammer

        Hi,

        I am trying to draw a polygon in my graphicsScene.
        The polygon exists of QPoints that are stored inside qv_points (qv = QVector)

        void graphicsView::addPoly()
        {
            qDebug() << "Add polygon";
        
            QPolygonF polygon;
            polygon << qv_points;
        
            QBrush brush;
            brush.setColor(GREEN);
        
            QGraphicsPolygonItem *newPoly = new QGraphicsPolygonItem();
            newPoly->setPolygon(polygon);
            newPoly->setBrush(QBrush(GREEN, Qt::SolidPattern));
            scene->addItem(newPoly);
        }
        

        It comes into the function since I can see "Add polygon" in the debugger.
        Does anyone know why my polygon is not being drawn?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @hobbyProgrammer
        I know nothing about this area, but is it right that your QPolygonF polygon goes out of scope? Is setPolygon(polygon) going to copy it? Yep, @mrjj says it will do that!

        Or, does your polygon (qv_points) have the right number of sensible points?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Hi
          Check the actual points as the code works fine.

           QPolygonF polygon;
              polygon  << QPointF( 10, 10 ) << QPointF( 0, 90 ) <<
                          QPointF( 40, 70 ) << QPointF( 80,110 ) << QPointF( 70, 20 );
          
              QGraphicsPolygonItem *newPoly = new QGraphicsPolygonItem();
              newPoly->setPolygon(polygon);
              newPoly->setBrush(QBrush(Qt::green, Qt::SolidPattern));
              scene->addItem(newPoly);
          

          alt text

          @JonB Good point but it will make a copy of the QPolygonF so its not out of scope issue in this case.
          But spot on with the qv_points.
          I think thye might be in scene coordinates or view and not local.

          H 1 Reply Last reply
          1
          • mrjjM mrjj

            Hi
            Check the actual points as the code works fine.

             QPolygonF polygon;
                polygon  << QPointF( 10, 10 ) << QPointF( 0, 90 ) <<
                            QPointF( 40, 70 ) << QPointF( 80,110 ) << QPointF( 70, 20 );
            
                QGraphicsPolygonItem *newPoly = new QGraphicsPolygonItem();
                newPoly->setPolygon(polygon);
                newPoly->setBrush(QBrush(Qt::green, Qt::SolidPattern));
                scene->addItem(newPoly);
            

            alt text

            @JonB Good point but it will make a copy of the QPolygonF so its not out of scope issue in this case.
            But spot on with the qv_points.
            I think thye might be in scene coordinates or view and not local.

            H Offline
            H Offline
            hobbyProgrammer
            wrote on last edited by hobbyProgrammer
            #4

            @mrjj Hi thanks.
            I found that it does fill the qv_points, but when it comes in the addPolygon function the qv_points is empty.

            I have absolutely no clue why. This is my code:
            addPolygon is called by using the shortcut Ctrl+N (new polygon).

            void graphicsView::mousePressEvent(QMouseEvent *event)
            {
            
            
                if(event->button() == Qt::LeftButton)
                {
                    bool point_exists = false;
            
                    for(int i = 0; i < qv_points.size(); i++)
                    {
                        if(qv_points.at(i).x() <= mapToScene(event->pos()).x()+2.5 &&
                           qv_points.at(i).x() >= mapToScene(event->pos()).x()-2.5 &&
                           qv_points.at(i).y() <= mapToScene(event->pos()).y()+2.5 &&
                           qv_points.at(i).y() >= mapToScene(event->pos()).y()-2.5)
                        {
                            point_exists = true;
                        }
                    }
                    if(point_exists)
                    {
                        qDebug() << "point exists";
                    }
                    else if(!point_exists || qv_points.isEmpty())
                    {
                        addPoint(mapToScene(event->pos()));
                    }
                }
            }
            
            void graphicsView::addPoly()
            {
                qDebug() << "Add polygon";
            
                qDebug() << qv_points;
                QPolygonF polygon;
                polygon << qv_points;
                qDebug() << polygon;
            
                QBrush brush;
                brush.setColor(GREEN);
            
                QGraphicsPolygonItem *newPoly = new QGraphicsPolygonItem();
                newPoly->setPolygon(polygon);
                newPoly->setBrush(QBrush(GREEN, Qt::SolidPattern));
                scene->addItem(newPoly);
            }
            
            void graphicsView::addPoint(QPointF pos)
            {
                qv_points << pos;
                qDebug() << qv_points;
                QGraphicsEllipseItem *newEllipse = new QGraphicsEllipseItem();
            
                double x,y;
                QPointF point = pos;
                x = point.x()-2.5;
                y = point.y()-2.5;
            
                newEllipse->setRect(QRectF(x,y,5,5));
                newEllipse->setBrush(QBrush(Qt::red, Qt::SolidPattern));
                newEllipse->setFlags(QGraphicsEllipseItem::ItemIsMovable | QGraphicsEllipseItem::ItemIsSelectable);
            
                //add item to scene
                scene->addItem(newEllipse);
            
                qDebug() << pos;
            }
            
            

            this is what the debugger says:

            QVector(QPointF(142,73))
            QPointF(142,73)
            QVector(QPointF(142,73), QPointF(122,305))
            QPointF(122,305)
            QVector(QPointF(142,73), QPointF(122,305), QPointF(289,323))
            QPointF(289,323)
            QVector(QPointF(142,73), QPointF(122,305), QPointF(289,323), QPointF(284,69))
            QPointF(284,69)
            Add polygon
            QVector()
            QPolygonF()
            
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #5

              Hi
              Its very good you use qDebug() but would be even nicer if you added some text so
              its easier to follow what is what.

              qDebug() <<"graphicsView::addPoint - qv_points: " << qv_points;

              Anyway, it looks very odd as list suddenly is empty.
              It looks all is inside same class so unless you call clear on it
              i see no way it suddenly goes empty.

              However, looking at the posted code, there is also nowhere - where you add to the qv_points list.

              H 2 Replies Last reply
              0
              • mrjjM mrjj

                Hi
                Its very good you use qDebug() but would be even nicer if you added some text so
                its easier to follow what is what.

                qDebug() <<"graphicsView::addPoint - qv_points: " << qv_points;

                Anyway, it looks very odd as list suddenly is empty.
                It looks all is inside same class so unless you call clear on it
                i see no way it suddenly goes empty.

                However, looking at the posted code, there is also nowhere - where you add to the qv_points list.

                H Offline
                H Offline
                hobbyProgrammer
                wrote on last edited by
                #6

                @mrjj is there anyway to debug like in visual studio where you can step over etc.?

                I would just like to see what qv_points does with each action.

                1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  Its very good you use qDebug() but would be even nicer if you added some text so
                  its easier to follow what is what.

                  qDebug() <<"graphicsView::addPoint - qv_points: " << qv_points;

                  Anyway, it looks very odd as list suddenly is empty.
                  It looks all is inside same class so unless you call clear on it
                  i see no way it suddenly goes empty.

                  However, looking at the posted code, there is also nowhere - where you add to the qv_points list.

                  H Offline
                  H Offline
                  hobbyProgrammer
                  wrote on last edited by
                  #7

                  @mrjj said in how to draw QGraphicsPolygonItem:

                  However, looking at the posted code, there is also nowhere - where you add to the qv_points list.

                  the first line of addPoint() is where the point is added to the qv_points.

                  void graphicsView::addPoint(QPointF pos)
                  {
                      qv_points << pos;
                  
                  mrjjM 1 Reply Last reply
                  0
                  • H hobbyProgrammer

                    @mrjj said in how to draw QGraphicsPolygonItem:

                    However, looking at the posted code, there is also nowhere - where you add to the qv_points list.

                    the first line of addPoint() is where the point is added to the qv_points.

                    void graphicsView::addPoint(QPointF pos)
                    {
                        qv_points << pos;
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @hobbyProgrammer
                    Hi
                    Ah i miss that as i was looking for append or similar :)

                    Yes, just start the app in debug mode (f5)
                    Then place a break point.
                    Then press F10 to step or F11

                    H 2 Replies Last reply
                    0
                    • mrjjM mrjj

                      @hobbyProgrammer
                      Hi
                      Ah i miss that as i was looking for append or similar :)

                      Yes, just start the app in debug mode (f5)
                      Then place a break point.
                      Then press F10 to step or F11

                      H Offline
                      H Offline
                      hobbyProgrammer
                      wrote on last edited by
                      #9

                      @mrjj oh thanks!

                      I got the error: Unable to create a debugging engine.
                      but I didn't have any debuggers for MSVC. I did have two for minGW so I added those kits to my project and now I can view them hopefully.

                      1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @hobbyProgrammer
                        Hi
                        Ah i miss that as i was looking for append or similar :)

                        Yes, just start the app in debug mode (f5)
                        Then place a break point.
                        Then press F10 to step or F11

                        H Offline
                        H Offline
                        hobbyProgrammer
                        wrote on last edited by
                        #10
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          hobbyProgrammer
                          wrote on last edited by hobbyProgrammer
                          #11

                          @mrjj
                          I managed to see that it shows up empty in the addPolygon function, but when I add a new point it still shows the old points in the debugger.

                          This is what I get in the debugger:

                          qv_points in addPoint:  QVector(QPointF(27,40))
                          qv_points in addPoint:  QVector(QPointF(27,40), QPointF(50,229))
                          qv_points in addPoint:  QVector(QPointF(27,40), QPointF(50,229), QPointF(151,236))
                          qv_points in addPoint:  QVector(QPointF(27,40), QPointF(50,229), QPointF(151,236), QPointF(152,147))
                          addPoly() qv_points:  QVector()
                          addPoly() qv_points:  QVector()
                          addPoly() qv_points:  QVector()
                          qv_points in addPoint:  QVector(QPointF(27,40), QPointF(50,229), QPointF(151,236), QPointF(152,147), QPointF(245,129))
                          
                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            hi
                            The debugger for visual studio is a separate download called
                            Debugging tools from MS.

                            When you press Ctrl+N , you are not creating a new instance of the
                            graphicsView or something like that ?

                            H 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              hi
                              The debugger for visual studio is a separate download called
                              Debugging tools from MS.

                              When you press Ctrl+N , you are not creating a new instance of the
                              graphicsView or something like that ?

                              H Offline
                              H Offline
                              hobbyProgrammer
                              wrote on last edited by
                              #13

                              @mrjj yes I did! Thank you so much.

                              I had this in the create actions

                              GraphicsView gv = new GraphicsView;
                              
                              
                                  addPolyAct = new QAction(tr("&Add Poly..."), this);
                                  addPolyAct->setShortcut(QKeySequence::New);
                                  connect(addPolyAct, SIGNAL(triggered()), gv , SLOT(addPoly()));
                              

                              since I promoted the widget inside the designer I changed it to:

                                  addPolyAct = new QAction(tr("&Add Poly..."), this);
                                  addPolyAct->setShortcut(QKeySequence::New);
                                  connect(addPolyAct, SIGNAL(triggered()), ui->graphicsView, SLOT(addPoly()));
                              

                              Now everything works perfectly!
                              Thank you so much.

                              mrjjM 1 Reply Last reply
                              1
                              • H hobbyProgrammer

                                @mrjj yes I did! Thank you so much.

                                I had this in the create actions

                                GraphicsView gv = new GraphicsView;
                                
                                
                                    addPolyAct = new QAction(tr("&Add Poly..."), this);
                                    addPolyAct->setShortcut(QKeySequence::New);
                                    connect(addPolyAct, SIGNAL(triggered()), gv , SLOT(addPoly()));
                                

                                since I promoted the widget inside the designer I changed it to:

                                    addPolyAct = new QAction(tr("&Add Poly..."), this);
                                    addPolyAct->setShortcut(QKeySequence::New);
                                    connect(addPolyAct, SIGNAL(triggered()), ui->graphicsView, SLOT(addPoly()));
                                

                                Now everything works perfectly!
                                Thank you so much.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #14

                                @hobbyProgrammer

                                Ok. Good. :)
                                It was my only guess
                                how on earth the points list could become
                                empty.
                                Sometimes Im a lucky guesser :)

                                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