5 * 1. Créer le fichier hello.pro :
6 * +------------------------------------------------------------+
8 * |CONFIG += qt debug |
9 * |SOURCES += hello.cc |
10 * +------------------------------------------------------------+
12 * 2. Créer le fichier Makefile avec la commande :
13 * $ qmake -makefile hello.pro
14 * ou tout simplement :
17 * 3. Compiler avec la commande :
24 #include <QApplication>
27 #include <QPaintEvent>
37 //============================================================
38 // DrawingAreaInterface
40 class DrawingAreaInterface {
42 static const int DEFAULT_WIDTH = 640;
43 static const int DEFAULT_HEIGHT = 480;
45 virtual ~DrawingAreaInterface() { }
47 virtual void setColor(float red, float green, float blue) = 0;
48 virtual void drawPoint(int x, int y) = 0;
49 virtual void drawLine(int x1, int y1, int x2, int y2) = 0;
51 virtual void wait() = 0;
52 virtual void waitAll() = 0;
55 //============================================================
60 static int instanceCount;
61 static int DEFAULT_ARGC;
62 static char *DEFAULT_ARGV[];
65 WindowCore(int &argc = DEFAULT_ARGC, char *argv[] = DEFAULT_ARGV);
69 int WindowCore::instanceCount = 0;
70 int WindowCore::DEFAULT_ARGC = 1;
71 char *WindowCore::DEFAULT_ARGV[] = { "class WindowCore", NULL };
73 WindowCore::WindowCore(int &argc, char *argv[])
76 new QApplication(argc, argv);
78 WindowCore::instanceCount++;
81 WindowCore::~WindowCore()
83 if (WindowCore::instanceCount == 0)
86 WindowCore::instanceCount--;
89 //============================================================
92 class QtDrawingArea: public DrawingAreaInterface,
93 public WindowCore, public QWidget {
95 static int visibleCount;
101 void init(int width, int height, const char *title);
104 void paintEvent(QPaintEvent *)
106 QPainter painter(this);
107 painter.drawImage(0, 0, *image);
110 void closeEvent(QCloseEvent *event)
112 QtDrawingArea::visibleCount--;
116 void keyPressEvent(QKeyEvent *event)
118 if (event->key() == Qt::Key_Escape) {
126 QtDrawingArea(int argc, char *argv[],
127 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
128 const char *title = NULL);
130 QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
131 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
132 const char *title = NULL);
134 QtDrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
135 const char *title = NULL);
141 return image->width();
146 return image->height();
149 void setColor(const QColor &color)
151 QPen pen(painter->pen());
153 painter->setPen(pen);
155 void setColor(float red, float green, float blue)
158 color.setRgbF(red, green, blue);
159 this->setColor(color);
162 void drawPoint(int x, int y)
164 painter->drawPoint(x, y);
168 void drawLine(int x1, int y1, int x2, int y2)
170 painter->drawLine(x1, y1, x2, y2);
176 qApp->sendPostedEvents(this, 0);
177 qApp->processEvents();
183 if (QtDrawingArea::visibleCount > 1)
184 while (this->isVisible())
185 qApp->processEvents(QEventLoop::WaitForMoreEvents);
186 else if (QtDrawingArea::visibleCount > 0)
192 if (QtDrawingArea::visibleCount > 0)
197 int QtDrawingArea::visibleCount = 0;
199 QtDrawingArea::QtDrawingArea(int argc, char *argv[],
200 int width, int height, const char *title)
201 : WindowCore(argc, argv)
204 init(width, height, title);
207 QtDrawingArea::QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
208 int width, int height, const char *title)
210 , QWidget(parent, flags)
212 init(width, height, title);
215 QtDrawingArea::QtDrawingArea(int width, int height, const char *title)
219 init(width, height, title);
222 QtDrawingArea::~QtDrawingArea()
228 void QtDrawingArea::init(int width, int height, const char *title)
231 this->setWindowTitle(title);
232 this->setFocusPolicy(Qt::StrongFocus);
233 image = new QImage(width, height, QImage::Format_RGB32);
234 image->fill(QColor(Qt::white).rgb());
235 this->setFixedSize(image->size());
236 QtDrawingArea::visibleCount++;
238 painter = new QPainter(image);
242 //============================================================
243 class DrawingArea: public DrawingAreaInterface {
249 DrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
254 return image->width();
259 return image->height();
262 void setColor(const QColor &color)
264 QPen pen(painter->pen());
266 painter->setPen(pen);
269 void setColor(float red, float green, float blue)
272 color.setRgbF(red, green, blue);
273 this->setColor(color);
276 void drawPoint(int x, int y)
278 painter->drawPoint(x, y);
281 void drawLine(int x1, int y1, int x2, int y2)
283 painter->drawLine(x1, y1, x2, y2);
288 DrawingArea::DrawingArea(int width, int height)
290 image = new QImage(width, height, QImage::Format_RGB32);
291 image->fill(QColor(Qt::white).rgb());
292 painter = new QPainter(image);
295 DrawingArea::~DrawingArea()
301 //============================================================
302 class DrawingThreadCore: public QThread {
305 virtual void runForReal() = 0;
308 void DrawingThreadCore::run()
313 //============================================================
314 class MainDrawinThread: public DrawingThreadCore {
319 void MainDrawingThread::runForReal()
321 // >>> insert main drawing code here <<<
324 //============================================================
325 int main(int argc, char *argv[])
327 QApplication application(argc, argv);
328 MainDrawingThread mainDrawingThread;
330 mainDrawingThread.start();
332 return application.exec();
335 //============================================================
338 /* paramètres par défaut */
348 void DrawingThread::run()
350 int x, y; /* le pixel considéré */
351 float cr, ci; /* le complexe correspondant */
352 float zr, zi; /* pour calculer la suite */
354 float pr, pi; /* taille d'un pixel */
355 float rouge, vert, bleu;
358 // QtDrawingArea fen(argc, argv, larg, haut);
360 pr = (Rmax - Rmin) / larg;
361 pi = (Imax - Imin) / larg;
364 for (x = 0; x < larg; x++) {
366 for (y = 0; y < larg; y++) {
370 for (i = 1; i <= maxiter; i++) {
373 if (zr2 + zi2 >= 4) {
374 /* |z| >= 2 : on sort de la boucle */
377 /* on calcule le z suivant */
381 /* on est sorti trop t
\e-bôt du for(...):
\e-A
382 on affiche le pixel d'un couleur en fonction
384 if (i <= maxiter / 2) {
385 /* entre rouge et vert */
386 vert = (2.0 * i) / maxiter;
389 } else if (i <= maxiter) {
390 /* entre vert et bleu */
392 bleu = (2.0 * i) / maxiter - 1.0;
394 } else /* (i > maxiter) */
395 rouge = vert = bleu = 0.0;
396 fen.setColor(rouge, vert, bleu);