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));
        QPainterPath rectPath;
        QRect fRect(200,70,100,50);
        QRect sRect(500,370,100,50);
        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));
        QLine test(fCP, sCP);
        QRect otrRect(50,50,100,100);

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


  • 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(,;
       QPoint pCP((parent.width()/2)+parent.left(),(parent.height()/2);
        QPoint cCP((child.width()/2)+child.left(), (child.height()/2);

  • 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

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

Log in to reply