我在我的项目中使用 QGraphicsScene/QGraphicsView 对
我对这对有性能问题。
我将我的自定义图形项目添加到场景中并通过 View 显示内容。之后我的自定义图形项绘制方法被场景连续调用(就像无限循环一样)。这使得 %25 的 CPU 使用率(场景中大约 400 个项目)。什么可能导致这种行为?

这是我的项目实现之一:

class LevelCrossingItem : public QGraphicsWidget
{
public:
    LevelCrossingItem(QString _id,qreal _x,qreal _y);
    ~LevelCrossingItem();
    QRectF boundingRect() const;
    QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint  = QSizeF()) const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /* = 0 */);
    void readStateBits();
    bool isClosed();
    bool isGateArmBroken();
    bool isOpenedDuringRouteTanzimCompleted();
    bool hasDataConsistencyWarning();
    int type() const {return Type;}
private slots:
    void setVisible(bool);
private:
    enum {Type = FIELDLEVELCROSSING};
    QString m_id;
    QString m_source;
    short m_closedState;
    short m_brokenGateArmState;
    short m_openedDuringRouteTanzimCompletedState;
    short m_dataConsistencyWarningState;
    QBitArray stateBitArray;
    qreal x,y;
    QSvgRenderer *renderer;
};

#include "levelcrossing.h"

LevelCrossingItem::LevelCrossingItem(QString _id,qreal _x,qreal _y):m_id(_id),x(_x),y(_y),stateBitArray(4)
{
    m_source = LEVELCROSSING_RESOURCE_PATH.arg("open");
    renderer = new QSvgRenderer;
    setStateArray(stateBitArray);
    setZValue(-0.5);
}

LevelCrossingItem::~LevelCrossingItem()
{
    delete renderer;
}

void LevelCrossingItem::setVisible(bool visible)
{
    QGraphicsItem::setVisible(visible);
}

QRectF LevelCrossingItem::boundingRect() const
{
    return QRectF(QPointF(x,y),sizeHint(Qt::PreferredSize));
}

QSizeF LevelCrossingItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
    return QSizeF(50,270);
}

void LevelCrossingItem::readStateBits()
{
    m_closedState = property("Closed").toInt();
    m_brokenGateArmState = property("Broken").toInt();
    m_openedDuringRouteTanzimCompletedState = property("OpenedOnRouteWarning").toInt();
    m_dataConsistencyWarningState = property("DataConsistencyWarning").toInt();

    stateBitArray.setBit(0,qvariant_cast<bool>(m_closedState));
    stateBitArray.setBit(1,qvariant_cast<bool>(m_brokenGateArmState));
    stateBitArray.setBit(2,qvariant_cast<bool>(m_openedDuringRouteTanzimCompletedState));
    stateBitArray.setBit(3,qvariant_cast<bool>(m_dataConsistencyWarningState));

    setStateArray(stateBitArray);
}

void LevelCrossingItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    readStateBits();

    m_closedState == Positive ? m_source = LEVELCROSSING_RESOURCE_PATH.arg("closed")
        : m_source = LEVELCROSSING_RESOURCE_PATH.arg("open");
    m_brokenGateArmState == Positive ? m_source = LEVELCROSSING_RESOURCE_PATH.arg("broken")
        : m_source = m_source;

    if(m_openedDuringRouteTanzimCompletedState == Positive)
    {
        setWarningVisible(OOR_WRN.arg(name()).arg(interlockingRegionId()),true);
        if(stateChanged())
            emit itemAlarmOccured(m_id,LevelCrossingIsOpenDuringTanzimCompleted);
    }
    else
         setWarningVisible(OOR_WRN.arg(name()).arg(interlockingRegionId()),false);

    if(m_dataConsistencyWarningState == Positive)
    {
        setWarningVisible(DC_WRN.arg(name()).arg(interlockingRegionId()),true);
        if(stateChanged())
            emit itemAlarmOccured(m_id,LevelCrossingDataConsistency);
    }
    else
        setWarningVisible(DC_WRN.arg(name()).arg(interlockingRegionId()),false);

    renderer->load(m_source);
    renderer->render(painter,boundingRect());
}

bool LevelCrossingItem::isClosed()
{
    return m_closedState == Positive;
}

bool LevelCrossingItem::isGateArmBroken()
{
    return m_brokenGateArmState == Positive;
}

bool LevelCrossingItem::isOpenedDuringRouteTanzimCompleted()
{
    return m_openedDuringRouteTanzimCompletedState == Positive;
}

bool LevelCrossingItem::hasDataConsistencyWarning()
{
    return m_dataConsistencyWarningState == Positive;
}

我从 xml 文件中读取 x 和 y 坐标。对于此项,x 和 y 坐标分别为 239,344

最佳答案

很可能您的图形项目在实现中存在错误。我有类似的行为,但我无法弄清楚究竟是什么原因造成的。我怀疑当您在边界矩形之外绘制时会发生这种情况。这会触发一些清理例程,从而导致重新绘制项目,然后循环。最终,我通过仔细检查我的自定义图形项的实现并确保:

  • 这些不是 MyGraphicsItem::boundingRect 之外的绘画。使用 painter->setClipRect(boundingRect())
  • MyGraphicsItem::shape 不与 MyGraphicsItem::boundingRect 交叉。
  • 如果您覆盖任何其他 QGraphicsItem 函数,请确保您的实现是正确的。

  • 希望这可以帮助。随意贴出你的图形项的源代码,这样会更容易找到问题。

    关于performance - QGraphicsScene/QGraphicsView 性能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4990140/

    10-12 23:57