Results 1 to 4 of 4

Thread: How to get colored QImage

  1. #1
    Join Date
    Aug 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to get colored QImage

    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

    Qt Code:
    1. void MyClass::open()
    2. {
    3. QString filename = QFileDialog::getOpenFileName(this,tr("Load File"),"",tr("Text Files (*.txt);;Images (*.png *.jpg)"));
    4.  
    5. if (filename != "") {
    6. /*QFile file(filename);
    7. if (!file.open(QIODevice::ReadOnly)) {
    8. QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
    9. return;}
    10.  
    11. QString contents = file.readAll().constData();
    12. ui.Textfeld -> setPlainText(contents);
    13. file.close();}*/
    14.  
    15. QImage image(filename);
    16. image = image.convertToFormat(QImage::Format_Indexed8); //shows the image in gray
    17. /*image = QImage(600, 453, QImage::Format_Indexed8);*/ // shows black and white stripes (why?)
    18.  
    19. QVector<QRgb> table(256);
    20. for(int i = 0; i < table.size(); i++)
    21. {table[i] = qRgb ( i,i,i );}
    22. image.setColorTable(table);
    23.  
    24. /*QRgb i;
    25. for ( int i=0; i<256; i++ ) //build color Table
    26. image.setColor(i,qRgb(i,i,i));*/
    27.  
    28. //image.fill(qRgb(255,255,255));
    29. //image.setColor( 19, qRgb(255,255,0) );
    30. //(image.scanLine(y) + x) = 19;
    31. /*QPixmap pixmap;
    32. pixmap = pixmap.fromImage(image);*/
    33.  
    34. ui.Display->setPixmap(QPixmap::fromImage(image));}
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 13th August 2012 at 09:52. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to get colored QImage

    qRgb ( i,i,i ) will always be gray!

  3. #3
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: How to get colored QImage

    And of course the colour map needs to be created _before_ conversion and passed as an argument to the conversion. How else would Qt know to what index to map each RGB pixel using a closest match principle.

    So, you need to get a ready-made colour map data from somewhere...

  4. #4
    Join Date
    Aug 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get colored QImage

    Okay.....you`re right.....but how to do it otherwise.....do i have to build a loop for each Rgb value......means instead of i,i,i, to use i for red, j for green and k for blue??? Is it right to use QRgb? Did I have to use a Vector or does QT have another possibility???
    And why do I get the black and white Stripes when I use: image = QImage(600, 453, QImage::Format_Indexed8???

    Would be very nice if someone can help a little bit more. I`m not a Guru but it`ll be nice to get one in a few years :-p ....greetz Nadja

Similar Threads

  1. Replies: 1
    Last Post: 9th August 2010, 19:44
  2. Write colored text
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2010, 12:55
  3. Two-colored cell in item view
    By UVV in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2009, 16:33
  4. Colored cursor
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 15th May 2008, 12:16
  5. Colored Buttons
    By Walsi in forum Newbie
    Replies: 33
    Last Post: 8th December 2007, 12:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.