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 //============================================================
244 /* paramètres par défaut */
254 int main(int argc, char *argv[])
256 int x, y; /* le pixel considéré */
257 float cr, ci; /* le complexe correspondant */
258 float zr, zi; /* pour calculer la suite */
260 float pr, pi; /* taille d'un pixel */
261 float rouge, vert, bleu;
264 QtDrawingArea fen(argc, argv, larg, haut);
266 pr = (Rmax - Rmin) / larg;
267 pi = (Imax - Imin) / larg;
270 for (x = 0; x < larg; x++) {
272 for (y = 0; y < larg; y++) {
276 for (i = 1; i <= maxiter; i++) {
279 if (zr2 + zi2 >= 4) {
280 /* |z| >= 2 : on sort de la boucle */
283 /* on calcule le z suivant */
287 /* on est sorti trop t
\e-bôt du for(...):
\e-A
288 on affiche le pixel d'un couleur en fonction
290 if (i <= maxiter / 2) {
291 /* entre rouge et vert */
292 vert = (2.0 * i) / maxiter;
295 } else if (i <= maxiter) {
296 /* entre vert et bleu */
298 bleu = (2.0 * i) / maxiter - 1.0;
300 } else /* (i > maxiter) */
301 rouge = vert = bleu = 0.0;
302 fen.setColor(rouge, vert, bleu);
315 for (int x = 0; x < fen.width(); x++)
316 for (int y = 0; y < fen.height(); y++)
319 float r = sqrt(dx * dx + dy * dy) / fen.width();
320 dx = fen.width() - 1 - x;
321 float g = sqrt(dx * dx + dy * dy) / fen.width();
322 dx = fen.width() / 2 - x;
323 dy = fen.height() - y;
324 float b = sqrt(dx * dx + dy * dy) / fen.height();
325 if (r > 1.0) r = 1.0;
326 if (g > 1.0) g = 1.0;
327 if (b > 1.0) b = 1.0;
328 fen.setColor(r, g, b);
334 fen.setColor(0.0, 0.0, 1.0);
335 for (int x = 0; x < 10; x++)
336 for (int y = 0; y < 10; y++)
341 fen.setColor(0.0, 0.0, 0.0);
342 for (int z = 100; z <= 300; z += 4) {
343 fen.drawLine(150, 150, z, 100);
344 fen.drawLine(150, 150, z, 300);
345 fen.drawLine(150, 150, 100, z);
346 fen.drawLine(150, 150, 300, z);
348 std::cerr << "OOOO\n";
351 std::cerr << "qwewet\n";