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. Drawing directly with QPainter and drawing QPixmap give different results

Drawing directly with QPainter and drawing QPixmap give different results

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qpainterqpixmapqquickpaintedit
2 Posts 2 Posters 2.2k 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
    mdma2
    wrote on last edited by mdma2
    #1

    There's a simple class based on QQuickPaintedItem:

    class PaintedItem : public QQuickPaintedItem
    {
        Q_OBJECT
    public:
        PaintedItem();
        void paint(QPainter *painter) override;
    };
    
    // ...
    
    PaintedItem::PaintedItem()
    {
        setRenderTarget(QQuickPaintedItem::FramebufferObject);
    }
    
    void PaintedItem::paint(QPainter *painter)
    {
        painter->drawRect(0, 0, 150, 150);
    
        QPixmap* m_pixmap = new QPixmap(width(), height());
        m_pixmap->fill(QColor("transparent"));
    
        QPainter painter2(m_pixmap);
        painter2.setPen(QColor("red"));
        painter2.drawRect(0, 0, 150, 150);
    
        painter->drawPixmap(0, 0, *m_pixmap);
    }
    

    The paint() function just does two things: draw a rectangle directly with QPainter and draw the QPixmap containing the same rectangle. But if I set the render target as FramebufferObject in the constructor, those rectangles doesn't match for some reason. If I comment this string, everything's OK.

    With FramebufferObject
    alt text
    Without FramebufferObject
    alt text

    Could you please explain me why does it happen and how to deal with it?

    DiracsbracketD 1 Reply Last reply
    0
    • M mdma2

      There's a simple class based on QQuickPaintedItem:

      class PaintedItem : public QQuickPaintedItem
      {
          Q_OBJECT
      public:
          PaintedItem();
          void paint(QPainter *painter) override;
      };
      
      // ...
      
      PaintedItem::PaintedItem()
      {
          setRenderTarget(QQuickPaintedItem::FramebufferObject);
      }
      
      void PaintedItem::paint(QPainter *painter)
      {
          painter->drawRect(0, 0, 150, 150);
      
          QPixmap* m_pixmap = new QPixmap(width(), height());
          m_pixmap->fill(QColor("transparent"));
      
          QPainter painter2(m_pixmap);
          painter2.setPen(QColor("red"));
          painter2.drawRect(0, 0, 150, 150);
      
          painter->drawPixmap(0, 0, *m_pixmap);
      }
      

      The paint() function just does two things: draw a rectangle directly with QPainter and draw the QPixmap containing the same rectangle. But if I set the render target as FramebufferObject in the constructor, those rectangles doesn't match for some reason. If I comment this string, everything's OK.

      With FramebufferObject
      alt text
      Without FramebufferObject
      alt text

      Could you please explain me why does it happen and how to deal with it?

      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by
      #2

      @mdma2
      Can you post the full code of this short example, so that it can be tried out immediately using simple copy/paste...

      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