Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [QQuickPaintedItem] Problem with drawing text and lines
QtWS25 Last Chance

[QQuickPaintedItem] Problem with drawing text and lines

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.3k Views
  • 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.
  • M Offline
    M Offline
    maksim1979
    wrote on last edited by
    #1

    Hello,

    I have a subclass of QQuickPaintedItem and paint method is quite simple
    @
    //setRenderTarget(QQuickPaintedItem::FramebufferObject );
    painter->setRenderHint(QPainter::TextAntialiasing);
    painter->setRenderHint(QPainter::Antialiasing);
    QPen pen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);

    painter->setPen(pen);
    painter->setBrush(Qt::NoBrush);
    
    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?

    !http://i.imgur.com/6iJ4VDy.png()!
    !http://i.imgur.com/AUETJKr.png()!

    Thanks a lot,
    Maxim

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maksim1979
      wrote on last edited by
      #2

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

      draw(imagePainter);
      
      painter->drawImage(image);
      

      }
      @

      But I'm not sure about performance with this approach

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mnaydenov
        wrote on last edited by
        #3

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

        do

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maksim1979
          wrote on last edited by
          #4

          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.

          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