Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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


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



  • This post is deleted!


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



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



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



  • Please somebody help.



  • @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());
    


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


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


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

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


Log in to reply