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. Can't seem to draw from the center of two rectangles....
Qt 6.11 is out! See what's new in the release blog

Can't seem to draw from the center of two rectangles....

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 4.2k 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.
  • J Offline
    J Offline
    JoeJoe_000
    wrote on last edited by JoeJoe_000
    #1

    Hello I can't see my line if I draw it from the center of two rectangles.......

    However If I draw line then a rectangle all that looks ok.......

    Can you tell me how to get my line to be under the rectangles?

    void MainWindow::paintEvent(QPaintEvent *event)
    {
        QPainter pathPainter(this);
        pathPainter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
        pathPainter.setBrush(Qt::yellow);
    
       
        QPainterPath rectPath;
        QRect fRect(200,70,100,50);
        rectPath.addRect(fRect);
        pathPainter.drawPath(rectPath);
    
     
    
        QRect sRect(500,370,100,50);
        rectPath.addRect(sRect);
        pathPainter.drawPath(rectPath);
    
        QPainter lineDrawer(this);
    
        QPoint fCP((fRect.width()/2),(fRect.height()/2));
        QPoint sCP((sRect.width()/2), (sRect.height()/2));
        lineDrawer.setPen(QPen(Qt::black, 1, Qt::SolidLine));
        lineDrawer.setBrush(Qt::yellow);
        QLine test(fCP, sCP);
         lineDrawer.drawLine(test);
    
    
        QRect otrRect(50,50,100,100);
        rectPath.addRect(otrRect);
        pathPainter.drawPath(rectPath);
    
    
        lineDrawer.drawLine(10,10,250,250);
    
    
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2
      QPoint fCP((fRect.width()/2),(fRect.height()/2));
      QPoint sCP((sRect.width()/2), (sRect.height()/2));
      

      This draw a single point :)
      You draw the first rectangle two times.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JoeJoe_000
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • J Offline
          J Offline
          JoeJoe_000
          wrote on last edited by
          #4

          I believe you're mistaken sir.

          QPoint doesnt draw anything it holds values for a point, points that can be used to draw a line.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5
            QPoint fCP((fRect.width()/2),(fRect.height()/2));
            QPoint sCP((sRect.width()/2), (sRect.height()/3));
            

            Now it draws a line.
            At first glance i don't see the point, but it's there ;)

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JoeJoe_000
              wrote on last edited by JoeJoe_000
              #6

              awww man...what world are you from?? :'(

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JoeJoe_000
                wrote on last edited by
                #7

                Please somebody help.

                D 1 Reply Last reply
                0
                • J JoeJoe_000

                  Please somebody help.

                  D Offline
                  D Offline
                  Devopia53
                  wrote on last edited by Devopia53
                  #8

                  @JoeJoe_000

                  QPoint fCP((fRect.width()/2),(fRect.height()/2));
                  QPoint sCP((sRect.width()/2), (sRect.height()/2));
                  

                  The fCP and sCP are the same point(50, 25) as @JoeJoe_000 mentioned. So it does not look good, but it appears as a dot on the screen.

                  If you want to draw a line connecting the centers of two rectangles, try the following:

                  painter.drawLine(fRect.center(), sRect.center());
                  
                  1 Reply Last reply
                  2
                  • J Offline
                    J Offline
                    JoeJoe_000
                    wrote on last edited by JoeJoe_000
                    #9

                    Thanks a lot. I didnt know about those two functions. I finally figured out my idiocy. Thanks.

                    //This does the same thing as painter.drawLine(fRect.center(), sRect.center());
                       QPoint pCP((parent.width()/2)+parent.left(),(parent.height()/2)+parent.top());
                        QPoint cCP((child.width()/2)+child.left(), (child.height()/2)+child.top());
                    
                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JoeJoe_000
                      wrote on last edited by
                      #10

                      Do you know how to specify which object/shape is at a high layer (drawing over) or lower layer (drawing under)?

                      jsulmJ 1 Reply Last reply
                      0
                      • J JoeJoe_000

                        Do you know how to specify which object/shape is at a high layer (drawing over) or lower layer (drawing under)?

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

                        @JoeJoe_000 I would say what you draw first is lower layer

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

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

                          Hi
                          When using QPainter there is no layers.
                          So what you draw last, will be on top.

                          1 Reply Last reply
                          1

                          • Login

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