Important: Please read the Qt Code of Conduct -

[QQuickPaintedItem] Problem with drawing text and lines

  • Hello,

    I have a subclass of QQuickPaintedItem and paint method is quite simple
    //setRenderTarget(QQuickPaintedItem::FramebufferObject );
    QPen pen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);

    painter->drawLine(100, 10, 100, 200);
    painter->drawLine(10, 50, 107, 50);
    painter->drawText(50, 40, "some text");


    The problem here is that with FramebufferObject render target the text is ok but lines width is not 1. If I change render target to Image(default), the text looks ugly but the lines are ok. Any thoughts how to fix that?


    Thanks a lot,

  • It's possible to use Imgae for drawing, something like that
    void paint(QPainter *painter)
    QImage image(width(), height(), Image::Format_ARGB32_Premultiplied);
    QPainter imagePainter(&image);



    But I'm not sure about performance with this approach

  • To get the lines to draw right you must use float coordinates and snap them to pixel center.

    Instead of

    @painter->drawLine(100, 10, 100, 200);@


    @painter->drawLine(100+0.5, 10+0.5, 100+0.5, 200+0.5);@

    If it still looks blurry, it is because your item coords are not snapped to pixel bounds. This is, make sure you round your x and y pos (of the item itself) to an int.

    DISCLAIMER all this comes from a ton of exp with GraphicsView framework, but it should apply to qquick as well, because it is how (Antialiased) drawing works - you must pixel align.

  • Yep, thanks a lot. I've tried float coordinates but it didn't help. But I found that if I disable antialiasing (remove this line painter->setRenderHint(QPainter::Antialiasing); ), the line looks ok.

Log in to reply