#include <QApplication>
#include <QtGui>
#include <QtOpenGL>
{
Q_OBJECT
public:
{
item = 0;
timeLine = 0;
}
private:
public:
void initAnimation()
{
if (item == 0) {
item->setRect(sceneRect());
addItem(item);
item->hide();
}
if (timeLine == 0) {
connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
}
timeLine->start();
}
public slots:
void onViewRectChanged
(QRectF r
) {
setSceneRect(r);
if (item)
item->setRect(r);
}
private slots:
void animate(qreal val)
{
(val < 1) ? item->show() : item->hide();
item->setPen(Qt::NoPen);
item
->setBrush
(QColor(0,
0,
0,
(1-val
)*255));
}
};
{
Q_OBJECT
public:
{
scene = new GraphicsScene();
}
view
->setOptimizationFlag
(QGraphicsView::DontAdjustForAntialiasing);
view->installEventFilter(this);
connect(this,
SIGNAL(viewRectChanged
(QRectF)), scene,
SLOT(onViewRectChanged
(QRectF)));
setCentralWidget(view);
}
{
if (obj
== view
&& event
->type
() == QEvent::Resize) { emit viewRectChanged(view->contentsRect());
}
}
protected:
{
if (event->key() == Qt::Key_Space)
scene->initAnimation();
}
private:
GraphicsScene *scene;
signals:
};
int main(int argc, char **argv)
{
MainWindow mw;
mw.resize(1024, 768);
mw.show();
return app.exec();
}
#include "main.moc"
#include <QApplication>
#include <QtGui>
#include <QtOpenGL>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
GraphicsScene(QObject *parent=0)
{
item = 0;
timeLine = 0;
}
private:
QGraphicsRectItem * item;
QTimeLine * timeLine;
public:
void initAnimation()
{
if (item == 0) {
item = new QGraphicsRectItem;
item->setRect(sceneRect());
addItem(item);
item->hide();
}
if (timeLine == 0) {
timeLine = new QTimeLine(750, this);
connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
}
timeLine->start();
}
public slots:
void onViewRectChanged(QRectF r)
{
setSceneRect(r);
if (item)
item->setRect(r);
}
private slots:
void animate(qreal val)
{
(val < 1) ? item->show() : item->hide();
item->setPen(Qt::NoPen);
item->setBrush(QColor(0, 0, 0, (1-val)*255));
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent=0)
{
scene = new GraphicsScene();
view = new QGraphicsView(scene);
if (QGLFormat::hasOpenGL()) {
view->setViewport(new QGLWidget);
}
view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
view->setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
view->installEventFilter(this);
connect(this, SIGNAL(viewRectChanged(QRectF)), scene, SLOT(onViewRectChanged(QRectF)));
setCentralWidget(view);
}
virtual bool eventFilter(QObject *obj, QEvent *event)
{
if (obj == view && event->type() == QEvent::Resize) {
emit viewRectChanged(view->contentsRect());
}
return QMainWindow::eventFilter(obj, event);
}
protected:
void keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space)
scene->initAnimation();
}
private:
GraphicsScene *scene;
QGraphicsView *view;
signals:
void viewRectChanged(QRectF);
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow mw;
mw.resize(1024, 768);
mw.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks