[英]Periodically redraw QQuickItem
我通過擴展QQuickItem並覆蓋了updatePaintNode()函數創建了一個自定義QML元素。
由於我需要沿着將實時增長的時間線繪制矩形,因此需要為每個新幀重新繪制GUI。
有沒有一種方法可以讓每個新幀定期執行updatePaintNode()函數?
我試過調用node->markDirty(QSGNode::DirtyForceUpdate)
,它不會導致updatePaintNode()函數被定期調用。
編輯:這是我的代碼如下所示:
QSGNode *PianoRoll::updatePaintNode(QSGNode *n, QQuickItem::UpdatePaintNodeData *data)
{
QSGGeometryNode *node = static_cast<QSGGeometryNode *>(n);
if (!node)
{
node = new QSGSimpleRectNode(boundingRect(), Qt::white);
}
node->removeAllChildNodes();
qreal msPerScreen = 10000;
qreal pitchesPerScreen = 128;
qreal x_factor = (qreal) boundingRect().width() / msPerScreen;
qreal y_factor = (qreal) boundingRect().height() / pitchesPerScreen;
for (unsigned int i = 0; i < m_stream->notes.size(); i++)
{
shared_ptr<Note> note = m_stream->notes.at(i);
qreal left = boundingRect().left() + note->getTime() * x_factor;
qreal top = boundingRect().top() + note->getPitch() * y_factor;
qreal width;
qreal height = y_factor;
if (note->getDuration() != 0)
{
width = note->getDuration() * x_factor;
}
else
{
// TODO
width = 500 * x_factor;
}
QRectF noteRectangle = QRectF(left, top, width, height);
node->appendChildNode(new QSGSimpleRectNode(noteRectangle, Qt::black));
}
node->markDirty(QSGNode::DirtyForceUpdate);
return node;
}
從updatePaintNode
的文檔中:
如果用戶已在項目上設置
QQuickItem::ItemHasContents
標志,則該函數作為QQuickItem::update()
的結果被調用。
您需要做兩件事:定期調用update()
並設置標志。 這就是全部。
如果您需要update()
的滴答聲源,則需要QQuickWindow::frameSwapped()
或類似的信號。 這會在每一幀,也就是每一幀都發出。 因此,這將起作用:
QSGNode * myNode = ...;
QObject::connect(window, &QQuickWindow::frameSwapped, [=](){ myNode->update(); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.