Results 1 to 3 of 3

Thread: display QImage on QLabel

  1. #1
    Join Date
    Sep 2008
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default display QImage on QLabel

    Hi! I am trying to display image on the label:

    Creating the image:

    Qt Code:
    1. static const QRgb s_rgnColors[256] =
    2. {
    3. (unsigned int)0x000000,
    4. (unsigned int)0xFF0000,
    5. (unsigned int)0x00FF00,
    6. (unsigned int)0xFFFF00,
    7. (unsigned int)0x0000FF,
    8. (unsigned int)0xFF00FF,
    9. (unsigned int)0x00FFFF,
    10. (unsigned int)0xFFFFFF,
    11.  
    12. // TODO: think about other colors
    13. (unsigned int)0x000000
    14.  
    15. };
    16.  
    17. QImage *m_imageFloor = new QImage(nWidth, nHeight, QImage::Format_Indexed8);
    18. m_imageFloor->setColorTable(QVector<QRgb>::fromStdVector(
    19. std::vector<QRgb>(s_rgnColors, s_rgnColors + _countof(s_rgnColors)))
    20. );
    21. m_imageFloor->fill(7);
    To copy to clipboard, switch view to plain text mode 

    and showing it on the label

    Qt Code:
    1. m_labelFloorPlan = new QLabel();
    2. m_labelFloorPlan->setSizePolicy(QSizePolicy::Expanding,
    3. QSizePolicy::Expanding);
    4. m_labelFloorPlan->setAlignment(Qt::AlignCenter);
    5. m_labelFloorPlan->setMinimumSize(400, 400);
    6. m_labelFloorPlan->setPixmap(QPixmap::fromImage(*m_imageFloor, Qt::AutoColor));
    7. setCentralWidget(m_labelFloorPlan);
    To copy to clipboard, switch view to plain text mode 

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

    Thank you.
    Last edited by jpn; 23rd September 2008 at 15:33. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: display QImage on QLabel

    Qt Code:
    1. #include <QtGui>
    2.  
    3. static const QVector<QRgb> colors = QVector<QRgb>()
    4. << qRgb( 0, 0, 0)
    5. << qRgb(255, 0, 0)
    6. << qRgb( 0, 255, 0)
    7. << qRgb(255, 255, 0)
    8. << qRgb( 0, 0, 255)
    9. << qRgb(255, 0, 255)
    10. << qRgb( 0, 255, 255)
    11. << qRgb(255, 255, 255);
    12.  
    13. int main(int argc, char* argv[])
    14. {
    15. QApplication app(argc, argv);
    16.  
    17. QImage image(320, 240, QImage::Format_Indexed8);
    18. image.setColorTable(colors);
    19. image.fill(2);
    20.  
    21. QLabel label;
    22. label.setPixmap(QPixmap::fromImage(image));
    23. label.show();
    24.  
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following 2 users say thank you to jpn for this useful post:

    akasi (23rd September 2008), TheIndependentAquarius (18th May 2011)

  4. #3
    Join Date
    Sep 2008
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: display QImage on QLabel

    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:

    Qt Code:
    1. m_pImageFloor = new QImage(nWidth, nHeight, QImage::Format_Indexed8);
    2. m_pImageFloor->setColorTable(s_rgnColors);
    3. m_pImageFloor->fill(0);
    4.  
    5. QBrush brush(Qt::SolidPattern);
    6. brush.setColor(Qt::darkGreen);
    7. QPainter painter(m_pImageFloor);
    8. painter.fillRect(0, 0, nWidth, nHeight, brush);
    To copy to clipboard, switch view to plain text mode 

    the painter does not become active..
    thanks

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. Replies: 3
    Last Post: 17th July 2008, 07:43
  3. QLabel ScaledContents ignored by style sheet?
    By WinchellChung in forum Newbie
    Replies: 3
    Last Post: 27th February 2008, 14:50
  4. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57
  5. QShared
    By sabeesh in forum Qt Programming
    Replies: 11
    Last Post: 11th October 2007, 12:40

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.