Calling canvas update event in js from QWebChannel
-
I first posted a question about this issue.
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);
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::0If 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.
When I call drawCanvas in js via QWebChannel, the data contains the correct values, but the canvas is not redrawn.
How Can I resolve it?
Thank for your time. -
var i = 0; window.addEventListener('resize', function(){ //++i; drawCanvas(++i); });
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.