5

我通过扩展 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;
}
4

1 回答 1

6

从以下文档updatePaintNode

QQuickItem::update()如果用户QQuickItem::ItemHasContents在项目上设置了标志,则调用该函数。

你需要做两件事:update()定期调用,并设置标志。这就是它的全部。

如果您需要 的报价来源update(),您需要QQuickWindow::frameSwapped()或类似的信号。这会在每一帧都发出,而且准确地说是每一帧。所以,这会起作用:

QSGNode * myNode = ...;

QObject::connect(window, &QQuickWindow::frameSwapped, [=](){ myNode->update(); });
于 2013-10-18T19:28:53.260 回答