[英]Periodically redraw QQuickItem
I have created a custom QML Element by extending QQuickItem and overriding the updatePaintNode() function. 我通过扩展QQuickItem并覆盖了updatePaintNode()函数创建了一个自定义QML元素。
Since I need to draw rectangles along a timeline which will grow in real-time I need the GUI to be redrawn for every new frame. 由于我需要沿着将实时增长的时间线绘制矩形,因此需要为每个新帧重新绘制GUI。
Is there a way to have the updatePaintNode() function be executed periodically for every new frame? 有没有一种方法可以让每个新帧定期执行updatePaintNode()函数?
I have tried calling node->markDirty(QSGNode::DirtyForceUpdate)
which did not cause the updatePaintNode() function to be called periodically. 我试过调用
node->markDirty(QSGNode::DirtyForceUpdate)
,它不会导致updatePaintNode()函数被定期调用。
Edit: this is what my code looks like: 编辑:这是我的代码如下所示:
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;
}
From the documentation of updatePaintNode
: 从
updatePaintNode
的文档中:
The function is called as a result of
QQuickItem::update()
, if the user has set theQQuickItem::ItemHasContents
flag on the item.如果用户已在项目上设置
QQuickItem::ItemHasContents
标志,则该函数作为QQuickItem::update()
的结果被调用。
You need to be doing both things: calling update()
periodically, and having the flag set. 您需要做两件事:定期调用
update()
并设置标志。 That's all there's to it. 这就是全部。
If you need a source of ticks for update()
, you want the QQuickWindow::frameSwapped()
or a similar signal. 如果您需要
update()
的滴答声源,则需要QQuickWindow::frameSwapped()
或类似的信号。 This gets emitted every frame, and exactly every frame. 这会在每一帧,也就是每一帧都发出。 So, this would work:
因此,这将起作用:
QSGNode * myNode = ...;
QObject::connect(window, &QQuickWindow::frameSwapped, [=](){ myNode->update(); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.