I'm trying to write PyQt code equivalent to the following C++ code, but I'm stuck. I'm relatively new to Python, and I'm having a hard time following the PyQt documentation for QImage.
I can create the GUI parts, but how do I set up the raw data and call the QImage constructor?

Qt Code:
  1. int main(int argc, char* argv[])
  2. {
  3. // Create the Qt application and GUI components
  4. QApplication app(argc, argv);
  5. QGraphicsView view(&canvas, &win);
  6.  
  7. unsigned char* imageData = new unsigned char[324];
  8.  
  9. // Generate some image data
  10. for(int i = 0; i < 324; i++)
  11. {
  12. imageData[i] = i % 250;
  13. }
  14.  
  15. // Create the colormap for the indexed image
  16. QVector<QRgb> colormap;
  17. for(int i = 0; i < 255; i++)
  18. {
  19. colormap.append(qRgb(i, i, i));
  20. }
  21.  
  22. QImage img(imageData, 18, 18, QImage::Format_Indexed8); // Create the image
  23. img.setColorTable(colormap); // Set up the colormap
  24. QPixmap pix = QPixmap::fromImage(img); // Convert it to a pixmap so we can display it
  25. canvas.addPixmap(pix); // Add the pixmap to the graphics canvas
  26.  
  27. win.show();
  28. app.exec();
  29. }
To copy to clipboard, switch view to plain text mode 

Here's the Python code I've got so far:
Qt Code:
  1. from PyQt4.QtGui import *
  2. from os import sys
  3.  
  4. app = QApplication(sys.argv)
  5.  
  6. # Create the window and the graphics view
  7. win = QMainWindow()
  8. canvas = QGraphicsScene()
  9. view = QGraphicsView(canvas, win)
  10. view.resize(200, 200)
  11.  
  12. # Create the image data - How?
  13. # Create the QImage - How?
  14.  
  15. # Create the colormap
  16.  
  17. pixmap = QPixmap.fromImage(img)
  18.  
  19. # Add the image to the canvas
  20. canvas.addPixmap(pixmap)
  21.  
  22. win.show()
  23. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode