Вот, вроде двигаются:

| Код | #ifndef SCENE_H #define SCENE_H
#include <QGraphicsScene> #include <QList>
class QGraphicsItemGroup;
class Scene: public QGraphicsScene { Q_OBJECT public: Scene(); public slots: void animate(); private: QList<QGraphicsItemGroup*> groups; };
#endif // SCENE_H
|
| Код | #include "scene.h"
#include <math.h>
#include <QGraphicsItem>
Scene::Scene() : QGraphicsScene(0) { setBackgroundBrush(QBrush(Qt::black)); QBrush brush(Qt::white); QPen pen(brush, 3); QList<QGraphicsItem*> items; // A items << addLine(50, 50, 90, 50, pen) << addLine(70, 20, 70, 50, pen) << addEllipse(70 - 10, 62 - 10, 20, 20, pen, brush); groups << createItemGroup(items); // B items.clear(); items << addLine(100, 50, 140, 50, pen) << addLine(120, 50, 120, 70, pen) << addEllipse(120 - 10, 60 - 10, 20, 20, pen, brush); groups << createItemGroup(items); // C items.clear(); items << addLine(200, 50, 260, 50, pen) << addLine(210, 40, 210, 60, pen) << addLine(250, 40, 250, 60, pen) << addEllipse(230 - 8, 60 - 8, 16, 16, pen, brush); groups << createItemGroup(items); // D items.clear(); items << addLine(300, 50, 330, 80, pen) << addLine(330, 80, 360, 50, pen) << addLine(330, 80, 330, 50, pen) << addEllipse(330 - 10, 80 - 10, 20, 20, pen, brush); groups << createItemGroup(items); }
double drand() { return (double)rand()/(double)RAND_MAX; }
void Scene::animate() { qreal stepSize = 5.0; foreach(QGraphicsItemGroup *group, groups) { group->translate(drand()*stepSize - 0.5*stepSize, drand()*stepSize - 0.5*stepSize); } }
|
| Код | #include <QApplication> #include <QGraphicsView> #include <QTimer>
#include "scene.h"
int main(int argc, char *argv[]) { QApplication a(argc, argv); Scene *scene = new Scene(); QGraphicsView *view = new QGraphicsView(scene); view->setRenderHint(QPainter::Antialiasing, true); view->resize(400, 300); view->show(); QTimer timer; QObject::connect(&timer, SIGNAL(timeout()), scene, SLOT(animate())); timer.setInterval(30); timer.start(); return a.exec(); }
|
|