3

私のアプリケーションには QMdiArea があり、その中に QGraphicsView 派生ビュー (GfxInteractiveView) のインスタンスを含むサブウィンドウが表示され、カスタム QGraphicsItem 派生アイテムを含むシーンを視覚化します。

/// An image item which is displayed as the background of a scene.
class GfxImageItem : public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT

protected:
    GfxInteractiveImgView *view_;
    QPixmap pixmap_;

    QList<GfxPointItem *> pointItems_;

public:
    GfxImageItem(GfxInteractiveImgView *view);

    // set the pixmap to the image loaded from this path
    bool load(const QString &path);

    // normally not overriden, here just for tracing 
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        QLOG("Painting image"); // homebrew macro for tracing
        QGraphicsPixmapItem::paint(painter, option, widget);
    }
};

/// A generated image drawn at the foreground of a scene. 
class GfxMapItem : public QGraphicsPixmapItem
{

public:
    GfxMapItem(QGraphicsItem *item);

    void regenerateMap();

    // same as GfxMapItem, here just for tracing
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};

ここまでは順調ですが、実際に何かを実行する paint() で完全にカスタム化された別のアイテム GfxPointItem もあります。これをシーンに追加すると、すべてのアイテムとしてコアの 1 つで CPU 使用率がフルに跳ね上がります。階層内で、ウィンドウが表示されているかカスタム項目がシーンに存在する限り継続する再描画ループに入ります。これが発生したときにスタックを垣間見ると、どの関数も paint() の呼び出しを担当していないことがわかります。イベントはイベント ループで生成されます。GfxPointItem のコードは次のとおりです。

/// A draggable item representing an analysis point on the map, drawn on top of the map.
class GfxPointItem : public QGraphicsObject
{
    Q_OBJECT

protected:
    GfxImageItem *imgParent_;
    GfxInteractiveImgView *view_;
    int size_, fontSize_;
    QColor color_, oldColor_;
    qreal paintScale_;
    QRectF boundingRect_;
    bool active_, static_;
    QStaticText pointText_, valueText_;

public:
    GfxPointItem(GfxImageItem *parent, GfxInteractiveImgView *view, const QPointF &pos);

    void setActive(bool arg);
    void setStatic(bool arg);
    void setColor(const QColor &color) { color_ = color; update(); }

    virtual QRectF boundingRect() const { return boundingRect_; }
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

signals:
    void changedPos(int index, QPointF newpos);

public slots:
    void setPaintScale(qreal value);
    void setPointText(const QString &text);
    void setValueText(const QString &text);

protected:
    void updateBoundRect();
    void updatePointText();
    QPoint valueTextPos() const;
    QPoint pointTextPos() const;
};


GfxPointItem::GfxPointItem(GfxImageItem *parent, GfxInteractiveImgView *view, const QPointF &pos, int index) : QGraphicsObject(parent),
    imgParent_(parent),
    view_(view),
    index_(index), size_(8), fontSize_(8), 
    color_(Qt::black),
    paintScale_(view->invscale()),
    drawLabel_(true), active_(false), static_(false), floatPrec_(false)
{
    QLOGX("Creating new at " << pos.x() << "," << pos.y() << ", index: " << index);
    setPos(pos);
    updatePointText();
    connect(view, SIGNAL(scaleChanged(qreal)), this, SLOT(setPaintScale(qreal)));
}

/// An inactive point wil not respond to hover events and will not be movable.
void GfxPointItem::setActive(bool arg)
{
    QLOGX("Setting active state: " << arg);
    active_ = arg;
    setAcceptHoverEvents(arg);
    setFlag(QGraphicsItem::ItemIsMovable, arg);
}

/// Set or disable static mode on point. In static mode, the point text is not updated when changing position, so it can retain a constant label.
void GfxPointItem::setStatic(bool arg)
{
    QLOGX("Setting static mode: " << arg);
    static_ = arg;
}

void GfxPointItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QLOGX("Painting point");
    static const int margin = 2;
    setScale(paintScale_);
    QPen pen;
    pen.setWidth(1);
    pen.setColor(color_);
    painter->setPen(pen);

    // paint the centerpoint marker (two crossed lines)
    painter->drawLine(QPointF(-size_, 0), QPointF( size_, 0));
    painter->drawLine(QPointF(0, -size_), QPointF(0,  size_));

    // the label box and the two static text lines inside
    pen.setWidth(0);
    painter->setPen(pen);

    QFont font;
    font.setPointSize(fontSize_);
    painter->setFont(font);

    QBrush brush(Qt::SolidPattern);
    brush.setColor(QColor(255, 255, 127)); // sand yellow
    painter->setBrush(brush);

    // point text size, value text size
    QSizeF pts = pointText_.size(), 
           vts = valueText_.size();

    // point text position, value text position
    QPoint vtp = valueTextPos(),
           ptp = pointTextPos();

    // point id and position label and value indicator label in a rectangular box
    int shift = (valueText_.text().isEmpty()) ? 0 : vts.height();
    QRectF rect(ptp.x()-margin, ptp.y(), std::max(pts.width(), vts.width())+margin, pts.height() + shift);
    painter->drawRect(rect);
    painter->drawStaticText(ptp, pointText_);
    painter->drawStaticText(vtp, valueText_);
}

void GfxPointItem::setPaintScale(qreal value) 
{ 
    QLOGX("Updating scale: " << value);
    paintScale_ = value; 
    update(); 
}

void GfxPointItem::setPointText(const QString &text)
{
    QLOGX("Setting text: " << text);
    pointText_.setText(text);
    updateBoundRect(); 
    update();
}

void GfxPointItem::setValueText(const QString &text) 
{ 
    QLOGX("Setting value text: " << text);
    valueText_.setText(text); 
    updateBoundRect(); 
    update();
}

void GfxPointItem::updateBoundRect()
{
    QLOGX("Updating bounding rect");
    boundingRect_.setRect(- size_, pointTextPos().y(), 
        (2 * size_) + std::max(pointText_.size().width(), valueText_.size().width()), 
        (2 * size_) + pointText_.size().height() + valueText_.size().height());
}

void GfxPointItem::updatePointText()
{
    QLOGX("Updating point text");
    pointText_.setText("P" + QString::number(index_ + 1) + " (" 
        + (floatPrec_ ? QString::number(pos().x()) : QString::number(std::floor(pos().x()))) + "," 
        + (floatPrec_ ? QString::number(pos().y()) : QString::number(std::floor(pos().y()))) + ")");

    updateBoundRect();
    update();
}

void GfxPointItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QLOGX("Mouse move");
    QPointF p = pos();
    QPointF ep = event->pos();

    QGraphicsItem::mouseMoveEvent(event);

    if (!static_) updatePointText();
    emit changedPos(index_, pos());
}

イベントループがアイテムを再描画し続ける理由がわかりません。私はこれに気付かないでしょうが、標準の QFileDialog::getExistingDirectory() を表示する際に問題が発生しました。これは、グラフィカルアイテムを含むウィンドウが表示されている間はペイントすることさえできません。メインスレッドがそこから離れ、フリーズします。その後、描画関数にトレース ステートメントを追加し、アプリケーションが何もしていないように見える数秒後に、ログファイルに数万のエントリを見つけました。タスク マネージャーでは、ウィンドウが表示されているとき (4 コア プロセッサ上) の CPU 使用率は約 25% であり、ウィンドウを閉じると 0 に低下します。

これらの再描画を強制するコードはありません。イベント ループをデバッグして、アプリケーションの速度を低下させ、フリーズを引き起こすこの動作の原因を見つけるにはどうすればよいですか?

ありがとう!

Qt バージョンは最新の 5.0.2 バイナリ バンドルであり、アプリケーションは x64 用の Visual C++ 2012 でコンパイルされています。

4

1 に答える 1

1

それを呼び出すsetScalepaintに接続しsetPaintScale、次に QWidget のupdate関数を呼び出し、その領域を無効にし、メイン イベント ループに戻ると再描画が発生するように思えます。

したがって、ペイント メソッドを呼び出すたびに、1 つのペイントが必要なイベントを取得することになります。

于 2013-06-27T09:01:17.023 に答える