Nadja
13th August 2012, 09:49
Hey, I want to open a .png using QFileDialog. Works fine for .txt & .png! Unfortunately the Imige is only shown in gray. Although reading the Docu and some examples i cant solve my problem. Try to build a color Table in a few ways but it doesn`t works. Using a QLabel to display the Image. Could anybody help? Sorry for my English. Isn`t the best. Nadja
void MyClass::open()
{
QString filename = QFileDialog::getOpenFileName(this,tr("Load File"),"",tr("Text Files (*.txt);;Images (*.png *.jpg)"));
if (filename != "") {
/*QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
return;}
QString contents = file.readAll().constData();
ui.Textfeld -> setPlainText(contents);
file.close();}*/
QImage image(filename);
image = image.convertToFormat(QImage::Format_Indexed8); //shows the image in gray
/*image = QImage(600, 453, QImage::Format_Indexed8);*/ // shows black and white stripes (why?)
QVector<QRgb> table(256);
for(int i = 0; i < table.size(); i++)
{table[i] = qRgb ( i,i,i );}
image.setColorTable(table);
/*QRgb i;
for ( int i=0; i<256; i++ ) //build color Table
image.setColor(i,qRgb(i,i,i));*/
//image.fill(qRgb(255,255,255));
//image.setColor( 19, qRgb(255,255,0) );
//(image.scanLine(y) + x) = 19;
/*QPixmap pixmap;
pixmap = pixmap.fromImage(image);*/
ui.Display->setPixmap(QPixmap::fromImage(image));}
}
void MyClass::open()
{
QString filename = QFileDialog::getOpenFileName(this,tr("Load File"),"",tr("Text Files (*.txt);;Images (*.png *.jpg)"));
if (filename != "") {
/*QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
return;}
QString contents = file.readAll().constData();
ui.Textfeld -> setPlainText(contents);
file.close();}*/
QImage image(filename);
image = image.convertToFormat(QImage::Format_Indexed8); //shows the image in gray
/*image = QImage(600, 453, QImage::Format_Indexed8);*/ // shows black and white stripes (why?)
QVector<QRgb> table(256);
for(int i = 0; i < table.size(); i++)
{table[i] = qRgb ( i,i,i );}
image.setColorTable(table);
/*QRgb i;
for ( int i=0; i<256; i++ ) //build color Table
image.setColor(i,qRgb(i,i,i));*/
//image.fill(qRgb(255,255,255));
//image.setColor( 19, qRgb(255,255,0) );
//(image.scanLine(y) + x) = 19;
/*QPixmap pixmap;
pixmap = pixmap.fromImage(image);*/
ui.Display->setPixmap(QPixmap::fromImage(image));}
}