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 is not rendered in Layout
Forum Updated to NodeBB v4.3 + New Features

QQuickPaintedItem is not rendered in Layout

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 445 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.
  • I Offline
    I Offline
    Idodoqdo
    wrote on last edited by Idodoqdo
    #1

    Hello everyone, I'm trying to render QQuickPaintedItem in QML. If I set its dimensions using width and height, then everything is fine, but if I put it in layout, then it stops being drawn

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import com.mycompany.messaging 1.0
    import QtQuick.Layouts 1.15
    
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        RowLayout {
            anchors.fill: parent
            GraphItem {
               id : gi
               objectName: "gI"
    //           Layout.preferredWidth: parent.width / 2
    //           Layout.preferredHeight:  parent.height / 2
    //           implicitWidth: 10
    //           implicitHeight: 10
               width: 1000
               height: 500
        //       width: parent.width
        //       height: parent.height
        //       anchors.fill: parent
            }
            Rectangle {
                color: "red"
                Layout.preferredWidth: parent.width / 2
                Layout.fillHeight: true
    
            }
        }
    
        Connections {
            target: video
    
            function onNewFrame(image, val) {
                gi.onNewFrame(image)
            }
        }
    }
    

    header

    class GraphItem : public QQuickPaintedItem
    {
        Q_OBJECT
        QML_ELEMENT
    
        Q_PROPERTY(VideoItem* videoItem READ getVideoItem WRITE setVideoItem)
    
    public:
        GraphItem(QQuickPaintedItem *parent = nullptr)
            : QQuickPaintedItem(parent) {
        }
    
        QRectF boundingRect() const override{
            return _rect;
        }
    
        void paint(QPainter *painter) override {
            qDebug() << "1111";
            painter->drawImage(_rect, _image);
        }
    
        VideoItem * getVideoItem() {
            return _videoItem;
        }
    
        void setVideoItem(VideoItem* nItem) {
            connect(nItem, &VideoItem::newFrame, this, &GraphItem::onNewFrame
                    /*,Qt::QueuedConnection*/);
            if (_videoItem) _videoItem->disconnect(_videoItem, Q_NULLPTR, this, Q_NULLPTR);
            _videoItem = nItem;
        }
    
    public slots:
        void onNewFrame(QImage image){
            _image = image;
    //        if(this->scene()!=Q_NULLPTR)this->scene()->update();
            update(_rect.toRect());
        }
    
    protected:
        void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) override
        {
            if(change == ItemSceneChange ){
             _rect = QRectF(0, 0, width(), height());
            }
            return QQuickPaintedItem::itemChange(change, value);
        }
    private:
    
        QImage _image;
        QRectF _rect;
        VideoItem * _videoItem;
    };
    
    dheerendraD 1 Reply Last reply
    0
    • I Idodoqdo

      Hello everyone, I'm trying to render QQuickPaintedItem in QML. If I set its dimensions using width and height, then everything is fine, but if I put it in layout, then it stops being drawn

      import QtQuick 2.15
      import QtQuick.Window 2.15
      import com.mycompany.messaging 1.0
      import QtQuick.Layouts 1.15
      
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          RowLayout {
              anchors.fill: parent
              GraphItem {
                 id : gi
                 objectName: "gI"
      //           Layout.preferredWidth: parent.width / 2
      //           Layout.preferredHeight:  parent.height / 2
      //           implicitWidth: 10
      //           implicitHeight: 10
                 width: 1000
                 height: 500
          //       width: parent.width
          //       height: parent.height
          //       anchors.fill: parent
              }
              Rectangle {
                  color: "red"
                  Layout.preferredWidth: parent.width / 2
                  Layout.fillHeight: true
      
              }
          }
      
          Connections {
              target: video
      
              function onNewFrame(image, val) {
                  gi.onNewFrame(image)
              }
          }
      }
      

      header

      class GraphItem : public QQuickPaintedItem
      {
          Q_OBJECT
          QML_ELEMENT
      
          Q_PROPERTY(VideoItem* videoItem READ getVideoItem WRITE setVideoItem)
      
      public:
          GraphItem(QQuickPaintedItem *parent = nullptr)
              : QQuickPaintedItem(parent) {
          }
      
          QRectF boundingRect() const override{
              return _rect;
          }
      
          void paint(QPainter *painter) override {
              qDebug() << "1111";
              painter->drawImage(_rect, _image);
          }
      
          VideoItem * getVideoItem() {
              return _videoItem;
          }
      
          void setVideoItem(VideoItem* nItem) {
              connect(nItem, &VideoItem::newFrame, this, &GraphItem::onNewFrame
                      /*,Qt::QueuedConnection*/);
              if (_videoItem) _videoItem->disconnect(_videoItem, Q_NULLPTR, this, Q_NULLPTR);
              _videoItem = nItem;
          }
      
      public slots:
          void onNewFrame(QImage image){
              _image = image;
      //        if(this->scene()!=Q_NULLPTR)this->scene()->update();
              update(_rect.toRect());
          }
      
      protected:
          void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) override
          {
              if(change == ItemSceneChange ){
               _rect = QRectF(0, 0, width(), height());
              }
              return QQuickPaintedItem::itemChange(change, value);
          }
      private:
      
          QImage _image;
          QRectF _rect;
          VideoItem * _videoItem;
      };
      
      dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      @Idodoqdo

      is paint(..) method is called ? Can you confirm.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      I 1 Reply Last reply
      0
      • dheerendraD dheerendra

        @Idodoqdo

        is paint(..) method is called ? Can you confirm.

        I Offline
        I Offline
        Idodoqdo
        wrote on last edited by
        #3

        @dheerendra e4e51346-4043-4a3d-9f5d-c15ce8b3692f-image.png
        all the difficulties arise when I set the size via layout

        GrecKoG 1 Reply Last reply
        0
        • I Idodoqdo

          @dheerendra e4e51346-4043-4a3d-9f5d-c15ce8b3692f-image.png
          all the difficulties arise when I set the size via layout

          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          @Idodoqdo I think you should use geometryChanged instead of itemChange. My guess is that itemChange is called only once where your size is null and then the layout resize your item but you are not aware.

          I 1 Reply Last reply
          2
          • GrecKoG GrecKo

            @Idodoqdo I think you should use geometryChanged instead of itemChange. My guess is that itemChange is called only once where your size is null and then the layout resize your item but you are not aware.

            I Offline
            I Offline
            Idodoqdo
            wrote on last edited by
            #5

            @GrecKo
            Thank you very much, it really helped

                void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override {
                    qDebug() << "new geo";
                    _rect = newGeometry;
                    QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
            //        update();
                }
            //    void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) override
            //    {
            //        if(change == ItemSceneChange ){
            //            qDebug() << "lol";
            //            _rect = QRectF(0, 0, width(), height());
            //        }
            //        return QQuickPaintedItem::itemChange(change, value);
            //    }
            
            1 Reply Last reply
            0
            • I Idodoqdo has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved