Results 1 to 8 of 8

Thread: QPixmap seems to be null but it isn't

  1. #1
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QPixmap seems to be null but it isn't

    Hi all,
    I've a strange problem setting a QPixmap on a QLabel. I call a thread which sets the QPixmap on the QLabel. That's the code I'm using:

    Qt Code:
    1. QPixmap *pxm = new QPixmap((QString("C:/image.jpg")));
    2. if(!pxm->isNull())
    3. {
    4. printf("Pixmap isn't null! %dx%d\n", this->width(), pxm->height());
    5. this->a->w->imageLabel->setImage(this->pxm);
    6. }
    7. else
    8. {
    9. printf("Pixmap is null!\n");
    10. }
    To copy to clipboard, switch view to plain text mode 


    If I put it in the run() function of the thread, it clears the image that the imageLabel was showing before (you see a gray background). If I put it in the costructor of the thread instead, it works. But in both it prints:
    "Pixmap isn't null! 320x274
    I set the image. 320x274 32 (183, 175, 172)"

    Thats the method setImage of imageLabel:
    Qt Code:
    1. void G2_ImageLabel::setImage(QPixmap *pxm)
    2. {
    3. QImage img = pxm->toImage();
    4. QRgb px = img.pixel(10, 10);
    5. printf("I set the image. %dx%d %d (%d, %d, %d)\n", pxm->width(), pxm->height(), pxm->depth(), qRed(px), qGreen(px), qBlue(px));
    6. this->setPixmap(*pxm);
    7. }
    To copy to clipboard, switch view to plain text mode 

    I don't know what I'm doing wrong. Please help me!

    Thank you, Dario

    (sorry for my english)

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap seems to be null but it isn't

    you can't use QPixmap in threads. use QImage insted.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap seems to be null but it isn't

    Quote Originally Posted by spirit View Post
    you can't use QPixmap in threads. use QImage insted.
    I'm afraid, it doesn't work anyway...

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap seems to be null but it isn't

    whe do you use these code in a thread?
    Qt Code:
    1. QPixmap *pxm = new QPixmap((QString("C:/image.jpg")));
    2. if(!pxm->isNull())
    3. {
    4. printf("Pixmap isn't null! %dx%d\n", this->width(), pxm->height());
    5. this->a->w->imageLabel->setImage(this->pxm);
    6. }
    7. else
    8. {
    9. printf("Pixmap is null!\n");
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap seems to be null but it isn't

    Quote Originally Posted by spirit View Post
    whe do you use these code in a thread?
    Qt Code:
    1. QPixmap *pxm = new QPixmap((QString("C:/image.jpg")));
    2. if(!pxm->isNull())
    3. {
    4. printf("Pixmap isn't null! %dx%d\n", this->width(), pxm->height());
    5. this->a->w->imageLabel->setImage(this->pxm);
    6. }
    7. else
    8. {
    9. printf("Pixmap is null!\n");
    10. }
    To copy to clipboard, switch view to plain text mode 
    If I use it in the costructor of the thread I haven't problems. If I use it in the run method it doesn't work, and I need to use it there..

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap seems to be null but it isn't

    first of all, as I said, you can use GUI in a thread and QPixmap also.
    use signals/slots or custom event for communication between threads.
    read more about multithreading in Qt.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: QPixmap seems to be null but it isn't

    Quote Originally Posted by satoshi View Post
    If I use it in the costructor of the thread I haven't problems.
    That's because the constructor of the QThread object is executed in the context of the main thread and not the worker thread that is not yet running.
    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.


  8. #8
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap seems to be null but it isn't

    Quote Originally Posted by spirit View Post
    first of all, as I said, you can use GUI in a thread and QPixmap also.
    use signals/slots or custom event for communication between threads.
    read more about multithreading in Qt.
    Thank you, that helped. Problem solved

    Quote Originally Posted by wysota View Post
    That's because the constructor of the QThread object is executed in the context of the main thread and not the worker thread that is not yet running.
    I know, my problem was that I wasn't understanding why it wasn't working in multi-threading...

    Thank you! Dario

Similar Threads

  1. QPixmap is null on other machines
    By sepehr in forum Installation and Deployment
    Replies: 6
    Last Post: 12th July 2011, 07:24
  2. Position in a QPixMap created from a QGraphicsScene
    By stevel in forum Qt Programming
    Replies: 8
    Last Post: 8th April 2009, 22:39
  3. QPixmap memory handling
    By Angelo Moriconi in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 15:37
  4. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18
  5. QPixmap and HBITMAP
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 16:24

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.