设置QGraphicsLineItem动画时出现问题



我有以下代码在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);

然而,在编译时,它只会立即绘制一行。我想这就是循环中的问题,但我不能确切地弄清楚。所以,如果有人能给我一个提示,我真的很感激。

您的直接问题在于插槽实现。循环变量必须移动到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();
}

相关内容

  • 没有找到相关文章

最新更新