{
setCacheMode(DeviceCoordinateCache);
_boundingRect
= QRectF(0,
0,
960,
220);
_animationSpeed = 0.5;
}
QRectF Item
::boundingRect() const {
return _boundingRect;
}
{
painter->setBrush(Qt::yellow);
painter->drawRoundedRect(2, 2, 84, 84, 4, 4);
painter
->setBrush
(QColor(50,
50,
50));
painter->drawRoundedRect(90, 2, 84, 84, 4, 4);
painter->drawRoundedRect(176, 2, 534, 84, 4, 4);
painter->drawRoundedRect(712, 2, 245, 84, 4, 4);
}
void Item::advance(int phase)
{
if (phase == 1)
{
moveBy(-(_animationSpeed), 0.0);
}
}
void Item::setAnimationSpeed(qreal value)
{
_animationSpeed = value;
}
Item::Item(QGraphicsItem * parent)
: QGraphicsItem(parent)
{
setCacheMode(DeviceCoordinateCache);
_boundingRect = QRectF(0, 0, 960, 220);
_animationSpeed = 0.5;
}
QRectF Item::boundingRect() const
{
return _boundingRect;
}
void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->setBrush(Qt::yellow);
painter->drawRoundedRect(2, 2, 84, 84, 4, 4);
painter->setBrush(QColor(50,50,50));
painter->drawRoundedRect(90, 2, 84, 84, 4, 4);
painter->drawRoundedRect(176, 2, 534, 84, 4, 4);
painter->drawRoundedRect(712, 2, 245, 84, 4, 4);
}
void Item::advance(int phase)
{
if (phase == 1)
{
moveBy(-(_animationSpeed), 0.0);
}
}
void Item::setAnimationSpeed(qreal value)
{
_animationSpeed = value;
}
To copy to clipboard, switch view to plain text mode
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene->setBackgroundBrush(Qt::black);
ui->graphicsView->setScene(scene);
ui->graphicsView->setSceneRect(0,0,960,230);
for (int i = 0; i < 50; i++)
{
Item *item = new Item();
item->setPos(item->boundingRect().width() * i, 0);
item->setAnimationSpeed(ui->horizontalSlider->value() / 10.0);
scene->addItem(item);
_items.append(item);
}
connect(timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
timer->start(1);
_elapsedTimer = new QElapsedTimer();
_elapsedTimer->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timer_timeout()
{
if (_elapsedTimer->hasExpired(8))
{
_elapsedTimer->start();
ui->graphicsView->scene()->advance();
}
}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
foreach(Item *i, _items)
i->setAnimationSpeed(value / 10.0);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene *scene = new QGraphicsScene();
scene->setBackgroundBrush(Qt::black);
ui->graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
ui->graphicsView->setScene(scene);
ui->graphicsView->setSceneRect(0,0,960,230);
for (int i = 0; i < 50; i++)
{
Item *item = new Item();
item->setPos(item->boundingRect().width() * i, 0);
item->setAnimationSpeed(ui->horizontalSlider->value() / 10.0);
scene->addItem(item);
_items.append(item);
}
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
timer->start(1);
_elapsedTimer = new QElapsedTimer();
_elapsedTimer->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timer_timeout()
{
if (_elapsedTimer->hasExpired(8))
{
_elapsedTimer->start();
ui->graphicsView->scene()->advance();
}
}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
foreach(Item *i, _items)
i->setAnimationSpeed(value / 10.0);
}
To copy to clipboard, switch view to plain text mode
Bookmarks