动画QGraphicsLineItem的问题

时间:2013-07-29 17:52:34

标签: c++ qt qt5

我有以下代码在QtCore 5.0中绘制一条动画线,所以它看起来像是用像素逐个画笔。

我的想法是使用一个计时器并将timeout()与一个调用QGraphicsLineItem :: setLine()的插槽连接,所以这里是我绘制线并创建SLOT move()以循环并逐渐绘制线的方式: myLine.h

#ifndef MYLINE_H
#define MYLINE_H

#include <QGraphicsLineItem>

class myLine : public QObject, public QGraphicsLineItem
{
   Q_OBJECT

public:
    myLine();
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

private slots:
    void mySlot();
private:
    QLineF thisline;

};
#endif // MYLINE_H

myLine.cpp

   myLine::myLine()
{
    thisline.setLine(0,0,50,50);
}

void myLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(QPen(Qt::red, 8, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
    painter->setBrush(QBrush(Qt::blue, Qt::DiagCrossPattern));
    painter->drawLine(thisline);
}


void myLine::mySlot()
{
    for (int i = 1; i < 100; i++)
    {
        QLineF line = this->line();
        line.setLine(0,0,50+i,50+i);
        update();
    }

}

然后调用GraphicsScene

scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->setSceneRect(0,0,700,700);
ui->graphicsView->setRenderHint(QPainter::Antialiasing);

myLine *line = new myLine();
scene->addItem(line);

timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), line, SLOT(mySlot()));
timer->start(100);

但是,编译时只会立即绘制一行。我猜这是问题在于循环,但我无法弄清楚。如果有人能给我一个提示,我真的很感激。

1 个答案:

答案 0 :(得分:0)

您的直接问题在于插槽实施。循环变量必须移动到MyLine类。 mySlot()必须在调用时迭代一次。也没有必要有一个成员,QGraphicsLineItem已经做到了。因此,最小化的返工代码如下所示。

#include <QApplication>
#include <QGraphicsLineItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QBasicTimer>

class MyLine : public QObject, public QGraphicsLineItem
{
    QBasicTimer m_timer;
    int m_i;
    void timerEvent(QTimerEvent * ev) {
        if (ev->timerId() == m_timer.timerId()) {
            setLine(0, 0, 50+m_i, 50+m_i);
            m_i ++;
        }
    }
public:
    MyLine() : m_i(0) {
        setPen(QPen(Qt::red, 8));
        m_timer.start(100, this);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    scene.addItem(new MyLine);
    QGraphicsView view(&scene);
    view.setRenderHint(QPainter::Antialiasing);
    view.setSceneRect(0, 0, 300, 300);
    view.show();
    return a.exec();
}
唉,你正在做的事情并没有充分利用Qt带来的所有功能。动画框架将完全满足您的需求。下面的示例代码。

#include <QApplication>
#include <QGraphicsObject>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPropertyAnimation>

class MyLine : public QGraphicsObject
{
public:
    MyLine(QGraphicsItem *parent = 0) : QGraphicsObject(parent) {}
    QRectF boundingRect() const { return QRectF(-60,-60,120,120); }
    void paint(QPainter * p, const QStyleOptionGraphicsItem *, QWidget *) {
        p->setPen(QPen(Qt::black, 8));
        p->drawEllipse(QPointF(0, 0), 50, 50);
        p->setPen(QPen(Qt::red, 8));
        p->drawLine(0, 0, 0, 50);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsObject * line = new MyLine;
    scene.addItem(line);
    QPropertyAnimation anim(line, "rotation");
    anim.setStartValue(0);
    anim.setEndValue(360);
    anim.setDuration(1000);
    anim.setLoopCount(-1); // forever
    anim.start();
    QGraphicsView view(&scene);
    view.setRenderHint(QPainter::Antialiasing);
    view.setSceneRect(-60, -60, 120, 120);
    view.show();
    return a.exec();
}