StevenB
31st July 2010, 04:24
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 (http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qimage.html).
I can create the GUI parts, but how do I set up the raw data and call the QImage constructor?
int main(int argc, char* argv[])
{
// Create the Qt application and GUI components
QApplication app(argc, argv);
QMainWindow win;
QGraphicsScene canvas;
QGraphicsView view(&canvas, &win);
unsigned char* imageData = new unsigned char[324];
// Generate some image data
for(int i = 0; i < 324; i++)
{
imageData[i] = i % 250;
}
// Create the colormap for the indexed image
QVector<QRgb> colormap;
for(int i = 0; i < 255; i++)
{
colormap.append(qRgb(i, i, i));
}
QImage img(imageData, 18, 18, QImage::Format_Indexed8); // Create the image
img.setColorTable(colormap); // Set up the colormap
QPixmap pix = QPixmap::fromImage(img); // Convert it to a pixmap so we can display it
canvas.addPixmap(pix); // Add the pixmap to the graphics canvas
win.show();
app.exec();
}
Here's the Python code I've got so far:
from PyQt4.QtGui import *
from os import sys
app = QApplication(sys.argv)
# Create the window and the graphics view
win = QMainWindow()
canvas = QGraphicsScene()
view = QGraphicsView(canvas, win)
view.resize(200, 200)
# Create the image data - How?
# Create the QImage - How?
# Create the colormap
pixmap = QPixmap.fromImage(img)
# Add the image to the canvas
canvas.addPixmap(pixmap)
win.show()
sys.exit(app.exec_())
I can create the GUI parts, but how do I set up the raw data and call the QImage constructor?
int main(int argc, char* argv[])
{
// Create the Qt application and GUI components
QApplication app(argc, argv);
QMainWindow win;
QGraphicsScene canvas;
QGraphicsView view(&canvas, &win);
unsigned char* imageData = new unsigned char[324];
// Generate some image data
for(int i = 0; i < 324; i++)
{
imageData[i] = i % 250;
}
// Create the colormap for the indexed image
QVector<QRgb> colormap;
for(int i = 0; i < 255; i++)
{
colormap.append(qRgb(i, i, i));
}
QImage img(imageData, 18, 18, QImage::Format_Indexed8); // Create the image
img.setColorTable(colormap); // Set up the colormap
QPixmap pix = QPixmap::fromImage(img); // Convert it to a pixmap so we can display it
canvas.addPixmap(pix); // Add the pixmap to the graphics canvas
win.show();
app.exec();
}
Here's the Python code I've got so far:
from PyQt4.QtGui import *
from os import sys
app = QApplication(sys.argv)
# Create the window and the graphics view
win = QMainWindow()
canvas = QGraphicsScene()
view = QGraphicsView(canvas, win)
view.resize(200, 200)
# Create the image data - How?
# Create the QImage - How?
# Create the colormap
pixmap = QPixmap.fromImage(img)
# Add the image to the canvas
canvas.addPixmap(pixmap)
win.show()
sys.exit(app.exec_())