3.6. Special Features (ImageIO, OpenGL, Mesa)

There are some special features in Qt that you should know about. They provide you with special features that are not normally included with toolkits.

3.6.1. ImageIO

This special feature adds support for some image file formats. It opens and saves pictures for you.

3.6.1.1. Using ImageIO

The ImageIO feature adds support for many graphics file formats. The QImage widget uses this feature to open and save graphics files.

3.6.1.2. A Sample ImageIO Program

Listing 3.11 shows you how it works. The program will open and display a JPEG image (shown in Figure 3.5):


Example 3.11. imageio.cpp: An Image Viewer Program

   1 
   2  1: #include <kapp.h>
   3  2: #include <qwidget.h>
   4  3: #include <qpainter.h>  // The QPainter class draws graphics on widgets.
   5  4: #include <qimage.h>
   6  5: 
   7  6: class MyWindow : public QWidget
   8  7: {
   9  8:   public:
  10  9:     // Constructor for the window, just call the QWidget constructor
  11 10:     MyWindow() : QWidget() { }
  12 11:   protected:
  13 12:     void paintEvent(QPaintEvent *);
  14 13: };
  15 14: 
  16 15: // This function is called when the window area must be updated.
  17 16: // Load and view the image
  18 17: void MyWindow::paintEvent(QPaintEvent *ev)
  19 18: {
  20 19:   // Load the image that we want to show
  21 20:   QImage image;
  22 21:   if (image.load("test.jpg", 0))  // If the image was loaded,
  23 22:   {                               // Show the image.
  24 23:     // Draw graphics in this window
  25 24:     QPainter paint(this);
  26 25:     // Draw the image we loaded on the window
  27 26:     paint.drawImage(0, 0, image, 0, 0,
  28 27:                     image.width(), image.height());
  29 28:   }
  30 29: }
  31 30: 
  32 31: 
  33 32: int main(int argc, char **argv)
  34 33: {
  35 34:   KApplication app(argc, argv)
  36 35:   // Create the window
  37 36:   MyWindow window;
  38 37:   app.setMainWidget(&window);
  39 38:   window.setCaption("ImageIO Example");
  40 39:   window.setGeometry(100,100,300,300);
  41 40:   window.show();
  42 41:   return app.exec();
  43 42: }
  44 


Figure 3.5. Showing a JPEG Picture with ImageIO.


To compile the program, use the following commands:


   1 
   2 g++ -c -I$QTDIR/include imageio.cppg++ -o iio imageio.o -L$QTDIR/lib -lqt -lqimgio -ljpeg

This feature is useful in all programs that need to load or store pictures. Image viewers and paint programs are two kinds of programs that may benefit from ImageIO.

3.6.2. OpenGL, Mesa

OpenGL is an API for 2D and 3D graphics programming. It is quite useful, but you must buy a license before you may develop an OpenGL program.

Mesa is a free library with similar functions. You can compile and run most OpenGL programs with Mesa.

If you want to learn more about the OpenGL language, I recommend either the OpenGL Web site, (http://www.opengl.org), or the Mesa Web site, (http://www.mesa3d.org).

3.6.2.1. The QGL Widget

If you want to create OpenGL programs, I recommend the QGL widget. QGL is a widget that enables OpenGL code in Qt programs. The OpenGL compatibility in QGL comes from Mesa. The QGL widget has three virtual member functions, into which you put your OpenGL code.

  • initializeGL() is called first. In this function you write the code that sets up the OpenGL rendering.

  • paintGL() is called when the graphics must be drawn. This is where you put all the code that draws things on the screen.

  • resizeGL(int width, int height) is called when the widget is resized. If you want to respond to resize events, this is where you do it.

Note

You must install the Mesa or OpenGL libraries before you can use the OpenGL special feature in Qt. The Mesa libraries can be downloaded from http://www.mesa3d.org

3.6.2.2. A Sample OpenGL Program

Listing 3.12 demonstrates the OpenGL special feature.


Example 3.12. main.cpp: OpenGL Program for Qt

   1 
   2 #include <kapp.h>
   3 #include <qgl.h>
   4 
   5 // The QGLWidget is a QWidget with support for OpenGL
   6 class MyWindow : public QGLWidget
   7 {
   8   public:
   9     MyWindow() : QGLWidget() { }
  10   protected:
  11     // The functions where you put your OpenGL code.
  12     void initializeGL();     // The start code for the widget
  13     void resizeGL(int, int); // The widget is resized
  14     void paintGL();          // Redraw the graphics.
  15 };
  16 
  17 
  18 void MyWindow::initializeGL()
  19 {
  20   glClearColor(1.0, 1.0, 1.0, 1.0);
  21   glMatrixMode(GL_PROJECTION);
  22   glLoadIdentity();
  23   glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
  24 }
  25  
  26 void MyWindow::paintGL()
  27 {
  28   glClear(GL_COLOR_BUFFER_BIT);
  29 
  30   glColor3f(0.0, 0.1, 0.4);
  31 
  32   glBegin(GL_POLYGON);
  33   glVertex3f(3.0, 5.5, 0.0);
  34   glVertex3f(5.0, 8.0, 0.0);
  35   glVertex3f(7.0, 5.5, 0.0);
  36   glEnd();
  37 
  38   glBegin(GL_LINE_LOOP);
  39   glVertex3f(3.0, 4.5, 0.0);
  40   glVertex3f(5.0, 2.0, 0.0);
  41   glVertex3f(7.0, 4.5, 0.0);
  42   glEnd();
  43 
  44   glFlush();
  45 }
  46 
  47 void MyWindow::resizeGL(int w, int h)
  48 {
  49   glViewport(0, 0, (GLint)w, (GLint)h);
  50 }
  51 
  52 // A standard main() function
  53 int main(int argc, char **argv)
  54 {
  55   KApplication app(argc, argv, "QGL");
  56   MyWindow window;
  57   window.setGeometry(100,100,300,200);
  58   window.setCaption("OpenGL extension");
  59   app.setMainWidget(&window);
  60   window.show();
  61   return app.exec();
  62 }
  63 

This sample program is shown in Figure 3.6.


Figure 3.6. The OpenGL sample program.