Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: Problem displaying a QPixmap

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Problem displaying a QPixmap

    Good morning.
    I'm writing a camera viewer and I have problem displaying the frames.
    To display them I wrote a very little CameraRenderer ( inherited from QWidget ) widget.
    Here the code.

    .h
    Qt Code:
    1. #ifndef _CAMERARENDERER_H_
    2. #define _CAMERARENDERER_H_
    3.  
    4. #include <QWidget>
    5.  
    6. class CCameraRenderer : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. CCameraRenderer(QWidget* parent=0);
    12. ~CCameraRenderer();
    13.  
    14. void setImage(QPixmap*);
    15.  
    16. protected:
    17. /* paint event */
    18. virtual void paintEvent( QPaintEvent* event );
    19.  
    20. private:
    21. QPixmap* m_image;
    22. };
    23.  
    24. #endif //#ifndef _CAMERARENDERER_H_
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. #include "CameraRenderer.h"
    2. /*Qt*/
    3. #include <QPainter>
    4.  
    5. CCameraRenderer::CCameraRenderer(QWidget* parent)
    6. : QWidget(parent)
    7. {
    8. setFixedSize( QSize(640, 480) );
    9.  
    10. m_image = new QPixmap();
    11. }
    12.  
    13. CCameraRenderer::~CCameraRenderer()
    14. {
    15. }
    16.  
    17. void CCameraRenderer::setImage(QPixmap* image)
    18. {
    19. //m_image = QPixmap();
    20. m_image = image;
    21. }
    22.  
    23. void CCameraRenderer::paintEvent( QPaintEvent* pe )
    24. {
    25. QPainter p(this);
    26. p.setRenderHint( QPainter::Antialiasing, true );
    27.  
    28. // image
    29. p.drawPixmap( 0, 0, *m_image );
    30. }
    To copy to clipboard, switch view to plain text mode 


    then I have the application where I have a QTimer and periodically I get a frame from the camera that I would display. The class that manage the camera is CVistaCamera.
    Here the main code:

    .h
    Qt Code:
    1. class CVistaCamera : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CVistaCamera(QObject *parent = 0);
    7. ~CVistaCamera();
    8. int InitCamera();
    9. void CloseCamera();
    10.  
    11. VC_VARS* getStructPtr();
    12.  
    13. public slots:
    14. void onUpdate(); //<-- called in a timer event where I get a camera frame
    15.  
    16. signals:
    17. void sig_stopTimer();
    18. void sig_startTimer( int );
    19. void sig_updateDisplay( QPixmap* ); //<---should display the frame content
    20.  
    21. private:
    22. int m_fVcOpen;
    23.  
    24. VC_VARS* pVar;
    25.  
    26. unsigned char* pImageData;
    27. DWORD ImageSize;
    28. DWORD vcVideoSource;
    29.  
    30. QPixmap* pixmap;
    31. };
    32.  
    33. #endif //_VISTACAMERA_H_
    To copy to clipboard, switch view to plain text mode 

    and the main methods:
    Qt Code:
    1. void CVistaCamera::onUpdate() //called periodically with a QTimer
    2. {
    3.  
    4. VC_VARS* pVc = getStructPtr();
    5. // get a frame
    6. rc = VcGetImage( pImageData, ImageSize, &retLen );
    7.  
    8. if ( rc == VC_SUCCESS )
    9. {
    10. pixmap->loadFromData( pImageData, ImageSize );
    11. emit sig_updateDisplay( pixmap );
    12. }
    13. else
    14. {
    15. // error - camera disconnected?
    16. VcClose();
    17. m_fVcOpen = 0;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    the signal sig_updateDisplay is connected with a the slot onUpdateDisplay so:

    Qt Code:
    1. connect( m_vistaCamera, SIGNAL( sig_updateDisplay(QPixmap*) ), this, SLOT( onUpdateDisplay(QPixmap*) ) );
    To copy to clipboard, switch view to plain text mode 

    and here the slot:
    Qt Code:
    1. void VistaIrisCollector::onUpdateDisplay(QPixmap* pixmap )
    2. {
    3. ui.display->setImage( pixmap );
    4. ui.display->repaint();
    5. }
    To copy to clipboard, switch view to plain text mode 

    In the window I don't see nothing, I don't know why.
    I would have a help.
    Best Regards,
    Franco
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: Problem displaying a QPixmap

    Òscar Llarch i Galán

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by ^NyAw^ View Post
    Hi I don't use OpenCV to get images from my camera. My camera comes with its sdk.
    Meanwhile I changed the code in the onUpdate() so:

    Qt Code:
    1. // get the camera frame
    2. rc = VcGetImage( pImageData, ImageSize, &retLen );
    3. if ( rc == VC_SUCCESS )
    4. {
    5. if( (pixmap->loadFromData( (const uchar*)pImageData, ImageSize )) == true )
    6. emit sig_updateDisplay( pixmap );
    7. else
    8. qWarning("Error loading data");
    9. }
    To copy to clipboard, switch view to plain text mode 

    and I always get the error message so seems the problem is in the loadFromData.
    Which can be the problem? pImage data contains the frame data and ImageSize is 640*480.

    Regards
    Franco Amato

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: Problem displaying a QPixmap

    Hi,

    QPixmap tryies to read an image with a Header(BMP,GIF,PNG, ...).
    I supose that your SDK will get your image data directly. Try creating a QImage instead.
    The application that I show you uses a QImage as I'm explaining you. I know that you are not using OpenCV but it's the same problem.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    QPixmap tryies to read an image with a Header(BMP,GIF,PNG, ...).
    I supose that your SDK will get your image data directly. Try creating a QImage instead.
    The application that I show you uses a QImage as I'm explaining you. I know that you are not using OpenCV but it's the same problem.
    Hi,
    I changed the code so:

    Qt Code:
    1. // get the camera frame
    2. rc = VcGetImage( pImageData, ImageSize, &retLen );
    3. if ( rc == VC_SUCCESS )
    4. {
    5. QImage image(640, 480, QImage::Format_Indexed8); //grayscale image
    6. if( image.loadFromData((const uchar*)pImageData, 640*480) == true )
    7. qDebug("Ok");
    8. else
    9. qWarning("Error loading data");
    10. }
    To copy to clipboard, switch view to plain text mode 

    and again I always get error
    Franco Amato

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: Problem displaying a QPixmap

    Hi,

    Wich error are you getting? The image is not created or is not displayed?
    Think that if you are using 8 bit indexed image you have to create a palette and assing it to the QImage.
    Òscar Llarch i Galán

  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem displaying a QPixmap

    I can not display the image.
    The loadFromData returns false and I don't know why. The camera give to me frames at 640*480 grayscale frames,
    and I can not display them.
    Do you have an idea?
    Regards,
    Franco
    Last edited by franco.amato; 27th September 2010 at 19:51.
    Franco Amato

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem displaying a QPixmap

    loadFromData() expects a format header. What you need to do is to copy your pixel data directly into the image's pixel buffer (QImage::bits()).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by wysota View Post
    loadFromData() expects a format header. What you need to do is to copy your pixel data directly into the image's pixel buffer (QImage::bits()).
    Wysota hi,
    I didn't get you.
    I understood that QImage::bits() returns a pointer to the pixel data, but how can I set such pixel data?

    Thanx
    Franco Amato

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem displaying a QPixmap

    You can use memcpy().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by wysota View Post
    You can use memcpy().
    Is not clear for me, sorry.
    I have to copy from the frame bytes to where?
    And how can I pass the final buffer to the Qimage?

    Regards

    I did this:
    Qt Code:
    1. QImage image( 640, 480, QImage::Format_Indexed8);
    2. uchar* imagedata = image.bits();
    3. memcpy( imagedata, pImageData, ImageSize );
    4. pixmap->fromImage( image );
    5. emit sig_updateDisplay( pixmap );
    To copy to clipboard, switch view to plain text mode 

    but I alway get a gray image instead of the frame content.
    Is my code correct?

    Regards,
    Franco
    Last edited by wysota; 27th September 2010 at 21:07.
    Franco Amato

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by franco.amato View Post
    but I alway get a gray image instead of the frame content.
    Is my code correct?
    You didn't set a palette for your image.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by wysota View Post
    You didn't set a palette for your image.
    I tried this code:

    Qt Code:
    1. QImage image(640,480, QImage::Format_Indexed8);
    2. QVector<QRgb> colorTable;
    3. for(int i = 0; i < 256; i++)
    4. colorTable.push_back(qRgb(i,i,i));
    5. image.setColorTable(colorTable);
    6. uchar *imagedata = imagedata = image.bits();
    7. memcpy( imagedata, pImageData, ImageSize );
    8.  
    9. pixmap->fromImage( image );
    10. emit sig_updateDisplay( pixmap );
    To copy to clipboard, switch view to plain text mode 

    But again my images are all gray and no frame content is displayed so I imagine
    there are other problems :-(
    Last edited by franco.amato; 27th September 2010 at 22:06.
    Franco Amato

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem displaying a QPixmap

    There is no setPalette method in QImage.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by wysota View Post
    There is no setPalette method in QImage.
    Yes please give a look at my changed code in my last post
    Franco Amato

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem displaying a QPixmap

    You are setting all colors to gray shades so no wonder it all comes out gray. Also make sure the pixel data contains indexes from the palette and not actual colours.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by wysota View Post
    You are setting all colors to gray shades so no wonder it all comes out gray. Also make sure the pixel data contains indexes from the palette and not actual colours.
    Sorry but I can not solve this problem alone, please write to me 2 lines of code.
    Franco Amato

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem displaying a QPixmap

    Here you go:
    basic Code:
    1. 10 print "I have no idea what you want of me"
    2. 20 goto 10
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by wysota View Post
    Here you go:
    basic Code:
    1. 10 print "I have no idea what you want of me"
    2. 20 goto 10
    To copy to clipboard, switch view to plain text mode 
    Your way to help me is very funny but doesn't help me.

    You are setting all colors to gray shades so no wonder it all comes out gray.
    The camera gives grayscale images so what I have to set?

    Also make sure the pixel data contains indexes from the palette and not actual colours.
    I didn't get you with this suggestion.
    That's all!!
    Franco Amato

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem displaying a QPixmap

    Quote Originally Posted by franco.amato View Post
    Your way to help me is very funny but doesn't help me.
    Try expressing your problems better, maybe you will get better answers.

    The camera gives grayscale images so what I have to set?
    How should I know what you should set? If you are using some sdk, read its docs to see what kind of output it provides.

    I didn't get you with this suggestion.
    I'm not going to teach you what is the internal structure of indexed and full colour images. You have all the Web to find it out yourself. When you do that you will know what I am talking about.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Problem with displaying a QPixmap on a QImage
    By moatilliatta in forum Newbie
    Replies: 3
    Last Post: 7th September 2010, 17:30
  2. QTableView row-displaying problem
    By MasterBLB in forum Qt Programming
    Replies: 0
    Last Post: 21st June 2009, 11:10
  3. Problem while displaying the icons name
    By yuvaraj.yadav in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2009, 14:50
  4. [problem]displaying image with Pixmap
    By crazy_inf in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2008, 09:49
  5. Problem with displaying float value
    By mirluk in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2008, 14:33

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.