PDA

View Full Version : display QImage on QLabel



akasi
23rd September 2008, 12:02
Hi! I am trying to display image on the label:

Creating the image:



static const QRgb s_rgnColors[256] =
{
(unsigned int)0x000000,
(unsigned int)0xFF0000,
(unsigned int)0x00FF00,
(unsigned int)0xFFFF00,
(unsigned int)0x0000FF,
(unsigned int)0xFF00FF,
(unsigned int)0x00FFFF,
(unsigned int)0xFFFFFF,

// TODO: think about other colors
(unsigned int)0x000000

};

QImage *m_imageFloor = new QImage(nWidth, nHeight, QImage::Format_Indexed8);
m_imageFloor->setColorTable(QVector<QRgb>::fromStdVector(
std::vector<QRgb>(s_rgnColors, s_rgnColors + _countof(s_rgnColors)))
);
m_imageFloor->fill(7);


and showing it on the label



m_labelFloorPlan = new QLabel();
m_labelFloorPlan->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);
m_labelFloorPlan->setAlignment(Qt::AlignCenter);
m_labelFloorPlan->setMinimumSize(400, 400);
m_labelFloorPlan->setPixmap(QPixmap::fromImage(*m_imageFloor, Qt::AutoColor));
setCentralWidget(m_labelFloorPlan);


But the image does not appear :confused:
What am I doing wrong??

Thank you.

jpn
23rd September 2008, 16:03
#include <QtGui>

static const QVector<QRgb> colors = QVector<QRgb>()
<< qRgb( 0, 0, 0)
<< qRgb(255, 0, 0)
<< qRgb( 0, 255, 0)
<< qRgb(255, 255, 0)
<< qRgb( 0, 0, 255)
<< qRgb(255, 0, 255)
<< qRgb( 0, 255, 255)
<< qRgb(255, 255, 255);

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QImage image(320, 240, QImage::Format_Indexed8);
image.setColorTable(colors);
image.fill(2);

QLabel label;
label.setPixmap(QPixmap::fromImage(image));
label.show();

return app.exec();
}

akasi
23rd September 2008, 22:48
thank you for your answer but I still do not understand where is the problem in my code..
one more question:
why I cannot paint on the image which was created as QImage::Format_Indexed8

here is the example:



m_pImageFloor = new QImage(nWidth, nHeight, QImage::Format_Indexed8);
m_pImageFloor->setColorTable(s_rgnColors);
m_pImageFloor->fill(0);

QBrush brush(Qt::SolidPattern);
brush.setColor(Qt::darkGreen);
QPainter painter(m_pImageFloor);
painter.fillRect(0, 0, nWidth, nHeight, brush);

the painter does not become active..
thanks