Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling canvas update event in js from QWebChannel
Forum Updated to NodeBB v4.3 + New Features

Calling canvas update event in js from QWebChannel

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 211 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
    MyNameIsQt
    wrote on last edited by MyNameIsQt
    #1

    I first posted a question about this issue.

    link text

    I found out that this was an event issue and not a data issue sent to the WebChannel.

    // header
    class CBackground
    {
    private:
        _BACKGROUNDHEADER_ m_bkgndHeader;
    
        static QWebEngineView* s_webView;
        static QWebChannel* m_webChannel;
    
        QString htmlPath = QFileInfo(__FILE__).dir().absolutePath() + "/canvas.html";
    
    // .cpp
    
    bool CBackground::onDraw()
    {
       if(!m_pBkgndImage->isNull()){
    
            if (m_pBkgndImage->colorSpace().isValid()){
                m_pBkgndImage->convertToColorSpace(QColorSpace::SRgb);
            }
    
            QPoint point(0,0);
            point.rx() = m_bkgndHeader.Left;
            point.ry() = m_bkgndHeader.Top;
    
            QVariant imageVariant = QVariant::fromValue(*m_pBkgndImage);
    
            imageObject = new QObject();
            imageObject->setProperty("imageData", imageVariant);
    
            m_webChannel->registerObject("imageObject", imageObject);
    
            int imageSizeInBytes = m_pBkgndImage->bytesPerLine() * m_pBkgndImage->height();
    
            QByteArray imageDataArray(reinterpret_cast<const char*>(m_pBkgndImage->bits()), imageSizeInBytes);
    
            **QString jsCode = QString("drawCanvas('%1');").arg(imageDataArray.size());
            // s_webView is static variable 
            s_webView->page()->runJavaScript(jsCode);**
    
            if(!m_pBkgndImage->isNull()){
                delete m_pBkgndImage;
                m_pBkgndImage = nullptr;
            }
          
            m_webChannel->deregisterObject(imageObject);
            imageObject->deleteLater();
    
            return true;
    
        } else {
            QMessageBox::information(nullptr, "Drawing Image", "Failed 412");
            return false;
        }
    
        return false;
    }
    
     // canvas.html
    function drawCanvas(imageData){        
            console.error("data "+value);
            var canvas = document.getElementById("myCanvas");
            var context = canvas.getContext("2d");
    
            context.fillStyle = "yellow";
            context.fillRect(0, 0, canvas.width, canvas.height);
            context.font="48px serif";
            context.fillStyle = "black";
            context.fillText("Text "+imageData, 10, 50);
    
            if(imageData.length > 0 ){
                console.error("Image Data Length: " + (imageData ? imageData.length : "Undefined"));
            }
    
        }
        
        window.addEventListener('resize', drawCanvas);
    

    The last line of html is as follows:

    window.addEventListener('resize', drawCanvas);
    

    캡처44.JPG
    The 'drawCanvas' function runs and redraws the canvas only when the window size changes.
    But in the 'OnDraw()' function of background.cpp
    When calling the 'drawCanvas' function, the canvas is not updated.

      // The drawCanvas function of canvas.html is called, but the canvas is not updated.
     QString jsCode = QString("drawCanvas('%1');").arg(imageDataArray.size());
    //static QWebEngineView* s_webView;
     s_webView->page()->runJavaScript(jsCode);
    

    // application output result
    js: data 662672 // correct
    js: Image Data Length: 6
    js: Unhandled signal: imageObject::0

    If I check the application output, the data is correct. The problem is that the fillText of the canvas is not updated.

    The canvas screen is only updated when I call the drawCanvas function within the same js file.

    Untitled-01.jpg

    When I call drawCanvas in js via QWebChannel, the data contains the correct values, but the canvas is not redrawn.
    Untitled-1a.jpg

    How Can I resolve it?
    Thank for your time.

    M 1 Reply Last reply
    0
    • M MyNameIsQt

      I first posted a question about this issue.

      link text

      I found out that this was an event issue and not a data issue sent to the WebChannel.

      // header
      class CBackground
      {
      private:
          _BACKGROUNDHEADER_ m_bkgndHeader;
      
          static QWebEngineView* s_webView;
          static QWebChannel* m_webChannel;
      
          QString htmlPath = QFileInfo(__FILE__).dir().absolutePath() + "/canvas.html";
      
      // .cpp
      
      bool CBackground::onDraw()
      {
         if(!m_pBkgndImage->isNull()){
      
              if (m_pBkgndImage->colorSpace().isValid()){
                  m_pBkgndImage->convertToColorSpace(QColorSpace::SRgb);
              }
      
              QPoint point(0,0);
              point.rx() = m_bkgndHeader.Left;
              point.ry() = m_bkgndHeader.Top;
      
              QVariant imageVariant = QVariant::fromValue(*m_pBkgndImage);
      
              imageObject = new QObject();
              imageObject->setProperty("imageData", imageVariant);
      
              m_webChannel->registerObject("imageObject", imageObject);
      
              int imageSizeInBytes = m_pBkgndImage->bytesPerLine() * m_pBkgndImage->height();
      
              QByteArray imageDataArray(reinterpret_cast<const char*>(m_pBkgndImage->bits()), imageSizeInBytes);
      
              **QString jsCode = QString("drawCanvas('%1');").arg(imageDataArray.size());
              // s_webView is static variable 
              s_webView->page()->runJavaScript(jsCode);**
      
              if(!m_pBkgndImage->isNull()){
                  delete m_pBkgndImage;
                  m_pBkgndImage = nullptr;
              }
            
              m_webChannel->deregisterObject(imageObject);
              imageObject->deleteLater();
      
              return true;
      
          } else {
              QMessageBox::information(nullptr, "Drawing Image", "Failed 412");
              return false;
          }
      
          return false;
      }
      
       // canvas.html
      function drawCanvas(imageData){        
              console.error("data "+value);
              var canvas = document.getElementById("myCanvas");
              var context = canvas.getContext("2d");
      
              context.fillStyle = "yellow";
              context.fillRect(0, 0, canvas.width, canvas.height);
              context.font="48px serif";
              context.fillStyle = "black";
              context.fillText("Text "+imageData, 10, 50);
      
              if(imageData.length > 0 ){
                  console.error("Image Data Length: " + (imageData ? imageData.length : "Undefined"));
              }
      
          }
          
          window.addEventListener('resize', drawCanvas);
      

      The last line of html is as follows:

      window.addEventListener('resize', drawCanvas);
      

      캡처44.JPG
      The 'drawCanvas' function runs and redraws the canvas only when the window size changes.
      But in the 'OnDraw()' function of background.cpp
      When calling the 'drawCanvas' function, the canvas is not updated.

        // The drawCanvas function of canvas.html is called, but the canvas is not updated.
       QString jsCode = QString("drawCanvas('%1');").arg(imageDataArray.size());
      //static QWebEngineView* s_webView;
       s_webView->page()->runJavaScript(jsCode);
      

      // application output result
      js: data 662672 // correct
      js: Image Data Length: 6
      js: Unhandled signal: imageObject::0

      If I check the application output, the data is correct. The problem is that the fillText of the canvas is not updated.

      The canvas screen is only updated when I call the drawCanvas function within the same js file.

      Untitled-01.jpg

      When I call drawCanvas in js via QWebChannel, the data contains the correct values, but the canvas is not redrawn.
      Untitled-1a.jpg

      How Can I resolve it?
      Thank for your time.

      M Offline
      M Offline
      MyNameIsQt
      wrote on last edited by
      #2

      @MyNameIsQt

          var i = 0;
          window.addEventListener('resize', function(){
              //++i;
              drawCanvas(++i);
          });
      

      캡처123.JPG
      As shown in the picture below, the function called within the js file works exactly.
      However, when the drawCanvas function is called from cpp, the 'canvas' screen is not updated even if the correct data value is passed.

      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