Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++ Qt Qt Creator) Moving line

 

Moving line is a Qt example to do 2D graphics like this screenshot.

 

#include <cstdlib>
#include <QApplication>
#include <QGraphicsPathItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>

class QUpdatingPathItem : public QGraphicsPathItem {
  void advance(int phase) {
    if (phase == 0) return;
    int x = std::abs(std::rand()) % 100;
    int y = std::abs(std::rand()) % 100;
    QPainterPath p = path();
    p.lineTo(x, y);
    setPath(p);
  }
};

//Code adapted from
// * http://stackoverflow.com/questions/1173525/c-and-qt-problem-with-2d-graphics
//Thanks to Wuub:
// * http://stackoverflow.com/users/92874/wuub (Wuub's StackOverflow profile)
// * http://wuub.net (Wuub's homepage)
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QGraphicsScene s;
  QGraphicsView v(&s);

  QUpdatingPathItem item;

  s.addItem(&item);

  v.show();

  QTimer * const timer = new QTimer(&s);
  timer->connect(timer, SIGNAL(timeout()), &s, SLOT(advance()));
  timer->start(100);

  return a.exec();
}

 

Project file:

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-05-01T11:16:04
#
#-------------------------------------------------
TARGET = ProjectQtMovingLine
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict