Results 1 to 8 of 8

Thread: QImage * to QIcon

  1. #1
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QImage * to QIcon

    Hey there.

    I have array of custom structure object called Sprite, it looks like this:
    Qt Code:
    1. struct Sprite
    2. {
    3. Sprite() { image = new QImage(32, 32, SPRITE_FORMAT); }
    4. void setSprite(QImage *inImage) { image = inImage; }
    5. QImage *getSprite() { return image; }
    6.  
    7. QImage *image;
    8. uint8_t width, height, r, g, b;
    9. };
    To copy to clipboard, switch view to plain text mode 

    Now, I have QListWidget and I'd like to insert items with icons from that array, but I have problem. QPixmap::fromImage takes &QImage as parameter, and my function (getSprite()) returns pointer.

    I have following code (which crashes):
    Qt Code:
    1. foreach(Item *x, ItemsLoader->xmlMap)
    2. {
    3.  
    4. QString itemText = x->getName();
    5. itemText = itemText.left(1).toUpper() + itemText.mid(1);
    6. item->setText(itemText + " (ID: " + QString::number(x->getId()) + ")");
    7.  
    8. Sprite *sprite = ItemsLoader->sprMap[x->getId()];
    9. item->setIcon(QIcon(QPixmap::fromImage(*sprite->getSprite())));
    10.  
    11. ui->ItemsListWidget->addItem(item);
    12. }
    To copy to clipboard, switch view to plain text mode 

    I guess following line is causing application crash:
    Qt Code:
    1. item->setIcon(QIcon(QPixmap::fromImage(*sprite->getSprite())));
    To copy to clipboard, switch view to plain text mode 

    How do I convert correctly from QImage* to &QImage?

  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: QImage * to QIcon

    Hi, use
    Qt Code:
    1. item->setIcon(QIcon(QPixmap::fromImage(*(sprite->getSprite()))));
    To copy to clipboard, switch view to plain text mode 
    Otherwise you dereference sprite! But anyway, this code is hardly readable. Use temporary variables, thus you can maintain your code. And most compilers will optimize them away, to there should be no speed/performance impact.

  3. The following user says thank you to Lykurg for this useful post:

    Diath (24th December 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage * to QIcon

    Umm, still crashes. I tried to assign sprite->getSprite() to another variable to check if getSprite() doesn't cause crash and it doesn't so imho. it's still issue with converting.

  5. #4
    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: QImage * to QIcon

    then debug if in that case the returned pointer is valid. Also can you submit a backtrace of the the crash.

  6. #5
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage * to QIcon

    Not sure if that's what you meant, but there you have backtrace:
    Qt Code:
    1. Thread 5 (thread 3864.0x1284):
    2. #0 0x76e464f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
    3. #1 0x76e44c3c in ntdll!ZwDeleteValueKey () from C:\Windows\system32\ntdll.dll
    4. #2 0x751a1876 in SleepEx () from C:\Windows\system32\KernelBase.dll
    5. #3 0x00000000 in ?? ()
    6.  
    7. Thread 4 (thread 3864.0xa78):
    8. #0 0x76e5566e in ntdll!RtlAddAttributeActionToRXact () from C:\Windows\system32\ntdll.dll
    9. #1 0x76e30c3f in ntdll!RtlGetCurrentPeb () from C:\Windows\system32\ntdll.dll
    10. #2 0x76edcd38 in ntdll!NtAlertResumeThread () from C:\Windows\system32\ntdll.dll
    11. #3 0x76e30bd3 in ntdll!RtlGetCurrentPeb () from C:\Windows\system32\ntdll.dll
    12. #4 0x7e17f060 in ?? ()
    13. #5 0x00000000 in ?? ()
    14.  
    15. Thread 3 (thread 3864.0xd60):
    16. #0 0x76e464f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
    17. #1 0x76e45e9c in ntdll!ZwYieldExecution () from C:\Windows\system32\ntdll.dll
    18. #2 0x76e306ab in ntdll!RtlLargeIntegerShiftLeft () from C:\Windows\system32\ntdll.dll
    19. #3 0x75a71194 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
    20. #4 0x76e5b495 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
    21. #5 0x76e5b468 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
    22. #6 0x00000000 in ?? ()
    23.  
    24. Thread 2 (thread 3864.0x16b4):
    25. #0 0x76e464f4 in ntdll!LdrFindResourceEx_U () from C:\Windows\system32\ntdll.dll
    26. #1 0x76e45e6c in ntdll!ZwWriteRequestData () from C:\Windows\system32\ntdll.dll
    27. #2 0x76e2ef56 in ntdll!RtlIsValidIndexHandle () from C:\Windows\system32\ntdll.dll
    28. #3 0x75a71194 in KERNEL32!AcquireSRWLockExclusive () from C:\Windows\system32\kernel32.dll
    29. #4 0x76e5b495 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
    30. #5 0x76e5b468 in ntdll!RtlInsertElementGenericTableAvl () from C:\Windows\system32\ntdll.dll
    31. #6 0x00000000 in ?? ()
    32.  
    33. Thread 1 (thread 3864.0x15e4):
    34. #0 0x0066b515 in QImage::convertToFormat (this=0x22fcc0, format=QImage::Format_RGB32, flags={i = 0}) at image\qimage.cpp:3747
    35. #1 0x006915ce in QRasterPixmapData::createPixmapForImage (this=0xbe7cc48, sourceImage=@0x22fcc0, flags={i = 0}, inPlace=false) at image\qpixmap_raster.cpp:453
    36. #2 0x006906b5 in QRasterPixmapData::fromImage (this=0xbe7cc48, sourceImage=@0x22fd18, flags={i = 0}) at image\qpixmap_raster.cpp:152
    37. #3 0x006862ab in QPixmap::fromImage (image=@0x22fd18, flags={i = 0}) at image\qpixmap.cpp:2031
    38. #4 0x00402297 in QMapEditor::startup (this=0x93b92b8) at ..\QMapEditor\qmapeditor.cpp:99
    39. #5 0x00401876 in QMapEditor (this=0x93b92b8, parent=0x0) at ..\QMapEditor\qmapeditor.cpp:25
    40. #6 0x004013ac in qMain (argc=1, argv=0x93b3688) at ..\QMapEditor\main.cpp:8
    41. #7 0x00404a0a in WinMain@16 (instance=0x400000, prevInstance=0x0, cmdShow=10) at qtmain_win.cpp:131
    42. #8 0x004047d2 in main ()
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QImage * to QIcon

    I guess following line is causing application crash:
    Qt Code:
    1. item->setIcon(QIcon(QPixmap::fromImage(*sprite->getSprite())));
    To copy to clipboard, switch view to plain text mode 
    Why guess? Single step through the code and determine exactly where the crash is and what the value of the various pointers is. Can ItemsLoader->sprMap[x->getId()] ever return something that is NULL or invalid, i.e. if x->getId() is not in the map?

    Something for later: What happens to the QImage you allocate on the heap in the constructor when you setSprite() the first time?

  8. The following user says thank you to ChrisW67 for this useful post:

    Diath (24th December 2010)

  9. #7
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage * to QIcon

    I've added yesterday simple check if sprite or sprite->getSprite is NULL, like this:
    Qt Code:
    1. if(sprite == NULL || sprite->getSprite() == NULL)
    2. continue;
    To copy to clipboard, switch view to plain text mode 

    But it didn't help, application still crashes on item->setIcon~.

    I can assign sprite->getSprite() to variable thought.
    Qt Code:
    1. QImage *image = sprite->getSprite();
    To copy to clipboard, switch view to plain text mode 

    Really no idea what should I do.

    @Edit:
    I've tried to image->save() and output file is 0kb, seems like my function for reading sprites from file is failing somewhere - gotta fix that, thanks for all your help.
    Last edited by Diath; 24th December 2010 at 11:32.

  10. #8
    Join Date
    Jan 2011
    Location
    Poland
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage * to QIcon

    Hi.
    I do not know whether it is correct but try:
    Qt Code:
    1. QImage *image = qobject_cast<QImage*>(sprite->getSprite());
    To copy to clipboard, switch view to plain text mode 

    and:
    Qt Code:
    1. item->setIcon(QIcon(QPixmap::fromImage(image)));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Add pictures svg in QIcon
    By giorgik in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2010, 09:00
  2. Qicon
    By shihao in forum Qt Programming
    Replies: 11
    Last Post: 15th March 2010, 13:23
  3. How to get the QIcon fileneme in use.
    By gt.beta2 in forum Newbie
    Replies: 1
    Last Post: 25th February 2009, 15:30
  4. Replies: 2
    Last Post: 31st May 2006, 21:52
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

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.