QGraphicsScene / QGraphicsView性能

时间:2011-02-14 08:35:16

标签: performance qt qgraphicsview

我在我的项目中使用QGraphicsScene / QGraphicsView对 我对这对有性能问题。 我将自定义图形项目添加到场景中并使用视图显示内容。之后我的自定义图形项绘制方法不断被场景调用(就像无限循环一样)。这使得CPU使用率达到了%25(现场约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

1 个答案:

答案 0 :(得分:4)

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

来解决问题
  1. 这些不是MyGraphicsItem::boundingRect之外的绘画。使用painter->setClipRect(boundingRect())
  2. MyGraphicsItem::shape不会与MyGraphicsItem::boundingRect交叉。
  3. 如果您覆盖任何其他QGraphicsItem函数,请确保您的实现正确无误。
  4. 希望这会有所帮助。随意发布图形项的源代码,将更容易找到问题。